MATLAB Answers

Info

This question is closed. Reopen it to edit or answer.

Problem in running my code

1 view (last 30 days)
Maheen Fazal
Maheen Fazal on 14 Feb 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi Sir, kindly tell me where i am doing wrong??
TN=4;
n_vec=1:TN;
n=n_vec;
max=[0.32 0.51 0.112 1.3];
B=0.3;
Psi=0.6;
No=0.01;
tau=B./n;
for Max=max;
h=rand(1,TN);
Rhn=(tau*Psi*log(2))*(1+((max*h)/(tau*No)))
end

Answers (2)

KSSV
KSSV on 14 Feb 2019
Edited: KSSV on 14 Feb 2019
TN=4;
n_vec=1:TN;
n=n_vec;
themax=[0.32 0.51 0.112 1.3];
B=0.3;
Psi=0.6;
No=0.01;
tau=B./n;
for Max=themax;
h=rand(1,TN);
Rhn=(tau*Psi*log(2)).*(1+((themax.*h)./(tau*No)))
end
Read about element by element array operations. When you multiply arrays element by element use .*.
Also note that, don't name the variables on the name of inbuilt functions......I have renames variable max with themax. There is inbuilt function to find maximum called max.

madhan ravi
madhan ravi on 14 Feb 2019
Edited: madhan ravi on 14 Feb 2019
Don't name variable max because there is an inbuilt function named max(), preallocate Rhn for speed and efficiency:
TN=4;
n_vec=1:TN;
n=n_vec;
Max=[0.32 0.51 0.112 1.3];
B=0.3;
Psi=0.6;
No=0.01;
tau=B./n;
Rhn=zeros(TN);
for k=1:TN
h=rand(1,TN);
Rhn(k,:)=(tau*Psi*log(2))*(1+((Max(k)*h)/(tau*No)));
end
Rhn

Tags

Community Treasure Hunt

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

Start Hunting!