Didnot get the value after executing

2 views (last 30 days)
Maheen Fazal
Maheen Fazal on 14 Feb 2019
Answered: Walter Roberson on 14 Feb 2019
Hi Sir,after running this code ,MATLAB didnot display the Rhn answer and typing Rhn in the command window i get undefined variable.Sir kindly specify the error please
close all
clear all
TN=4;
n_vec=1:TN;
simulations=10000;
for n=n_vec
E=[];
for sim=1:simulations
B=0.3; %idle period
phi=(1-B)/n; %backscattering time
Bbn=20*1000; %transmission rate by backscatter communication
Rbn =(phi*Bbn); %no of bits transmitted by backscattering in busy period
PT=17*1000; %PT transmit power 17kW
del=0.6; %delta energy harvesting efficiency
No=0.01; %noise power
d= 8304.215; % distance from PT to ST ,5.16 miles, 8.304215km
sigma=2.7; %pathloss(db) in urban area cellular radio btw 2.7 ro 3.5
for r=TN;
g=rand(1,r);
PRn=((del*PT*g)./(1-d^sigma)*No); %generating negative value due to No
Kn=0.6; %transmission efficiency
Bandwidth=100*1000;
Psi=Kn*Bandwidth;
tau=B/n;
Ehn=((1-B-phi)*PRn);
E=[E Ehn];
sorted=sort(Ehn,'descend');
total=length(sorted);
half=(total/2);
max=sorted(1:half);
min=sorted(half+1:end);
end
for Max=1:max
rr=TN/2;
h=rand(1,rr);
Rhn=(tau*Psi*log(2))*(1+((max*h)/(tau*No)));
end
end
end
  2 Comments
Preethi
Preethi on 14 Feb 2019
Hi,
Check the value of 'max'
Maheen Fazal
Maheen Fazal on 14 Feb 2019
It is giving the value zero for max with the last for loop but when i remove it the max value get changed,i didnot understand how and why

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 14 Feb 2019
max and min are vectors. When you use a vector as a bound on a for loop, then only the first element in the vector is used.
Your for Max loop is overwriting Rhn in every iteration, so you might as well only use the last iteration.
Naming variables max or min is quite likely to lead to problems later when you try to use max() or min() as functions. At the very least it confuses readers of the code.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!