Onramp problem 14.1 and 14.2 plotting problems
3 views (last 30 days)
Show older comments
On both of these examples I have inputted the correct code - when compared to the answer. However in both examples it still says that I have an incorrect code. What am I doing wrong? (there are four lines on the plot, s is on the y axis, and it is dashed!!
3 Comments
kameshwar saini
on 26 Feb 2021
clc; clear variables; close all;
N = 10^5;
d1 = 0:10:100; d2 = 500; %Distances of users from base station (BS)
a1 = 0.75; a2 = 0.25; %Power allocation factors
eta = 4; %Path loss exponent
for d1=0:10:100
%Generate rayleigh fading coefficient for both users
h1 = sqrt(d1^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
h2 = sqrt(d2^-eta)*(randn(1,N)+1i*randn(1,N))/sqrt(2);
end
g1 = (abs(h1)).^2;
g2 = (abs(h2)).^2;
Pt = 0:2:40; %Transmit power in dBm
pt = (10^-3)*10.^(Pt/10); %Transmit power in linear scale
BW = 10^6; %System bandwidth
No = -174 + 10*log10(BW); %Noise power (dBm)
no = (10^-3)*10.^(No/10); %Noise power (linear scale)
p = length(Pt);
p1 = zeros(1,length(Pt));
p2 = zeros(1,length(Pt));
d = length(d1);
rate1 = 1; rate2 = 2; %Target rate of users in bps/Hz
for u = 1:p
%Calculate SNRs
gamma_1 = a1*pt(u)*g1./(a2*pt(u)*g1+no);
gamma_12 = a1*pt(u)*g2./(a2*pt(u)*g2+no);
gamma_2 = a2*pt(u)*g2/no;
%Calculate achievable rates
R1 = log2(1+gamma_1);
R12 = log2(1+gamma_12);
R2 = log2(1+gamma_2);
%Find average of achievable rates
R1_av(u) = mean(R1);
R12_av(u) = mean(R12);
R2_av(u) = mean(R2);
%Check for outage
for k = 1:N
if R1(k) < rate1
p1(u) = p1(u)+1;
end
if (R12(k) < rate1)||(R2(k) < rate2)
p2(u) = p2(u)+1;
end
end
end
figure;
plot(d1, R1_av, 'linewidth', 1.5); hold on; grid on;
I want to plot the graph between d1 and R1_av but I am getting blank plot . Please help me
Answers (1)
ANKUR KUMAR
on 26 Feb 2021
Few points I need to mention:
1) You are calculating R1_av as a function of u, so if you use the below chunk of code, it will plot the values of R1_av.
plot(1:p, R1_av, 'linewidth', 1.5); hold on; grid on;
2) I am amaze that why you wish to plot R1_av as a function of d1. d1 is Distances of users from base station. You are using the same variable in line 3 and line 6. Never do that. Never assign the pre assigned variable in a loop iterated variable, as it is not a good practice and hard to debug the code.
2 Comments
kameshwar saini
on 26 Feb 2021
Is there any possible way to plot the graph between d1 and R1_av? By modifying the code?
ANKUR KUMAR
on 26 Feb 2021
Why do you wish to plot d1 vs R1_av? Both are having different dimension, and that is why it is NOT possible to plot both. If still you wish to plot these two, you need to modify the code. First try to understand which variable R1_av dependent on? Dimension of both x and y variable MUST be same.
See Also
Categories
Find more on Plot Customization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!