Why does my for loop only output the last-time result

I want to calculate different received power for different receiver coordinates, but why does my loop only get the same result?
rx = 4;
ry = 8;
rz = 3;
h = 2;
% Lambertian emission order
n = 1;
ne = 1;
% detector physical area of a PD
A = 1e-4;
% FOV of a receiver
FOV = 95;
%% first transmitter calculation
TP1=[1 1 3]; % transmitter position
% Distance between transmitter and receiver
% reciver
x=linspace(1,rx,4);
y=linspace(1,ry,8);
z=linspace(1,rz,3);
for ii = 1:rx %1 to 4
for jj = 1:ry %1 to 8
RP=[x(ii) y(jj) 1]; % receiver position vector
h_a1(ii,jj)=0; % reflection from North face
DLOS = sqrt(dot(TP1-RP,TP1-RP));% distance between transmitter and receiver
cos_alphaLOS = abs(RP(3)-TP1(3))/DLOS;
cos_betaLOS = abs(TP1(3)-RP(3))/DLOS;
if abs(acosd(cos_betaLOS)) <= FOV
%receiver power
h_a1(ii,jj) = h_a1(ii,jj) + ((n+1) * A * (cos_alphaLOS ^ n) * cos_betaLOS * Ps) / 2 * pi * (DLOS ^ 2);
end
end
end

 Accepted Answer

DGM
DGM on 29 Oct 2021
Edited: DGM on 29 Oct 2021
Observe this expression:
h_a1(ii,jj) = h_a1(ii,jj) + ((n+1) * A * (cos_alphaLOS ^ n) * cos_betaLOS * Ps) / 2 * pi * (DLOS ^ 2);
Reduce it to the terms which are ostensibly variable within the scope of the loop:
h_a1(ii,jj) = cos_alphaLOS * cos_betaLOS * DLOS^2;
Substitute the expressions for the first two variables:
h_a1(ii,jj) = abs(RP(3)-TP1(3))/DLOS * abs(TP1(3)-RP(3))/DLOS * DLOS^2;
Simplify:
h_a1(ii,jj) = abs(RP(3) - TP1(3))^2;
Which is constant within the scope of the loop. The entire expression is constant.

3 Comments

Thank you for your reply.
Do you mean to replace the variable name with a formula? So does the formula become the following?
h_a1(ii,jj) = h_a1(ii,jj) + ((n+1) * A * ((abs(RP(3)-TP1(3))/DLOS) ^ n) * (abs(TP1(3)-RP(3))) * Ps)...
/ 2 * pi * ((sqrt(dot(TP1-RP,TP1-RP))) ^ 2);
Unfortunately, no. I was merely identifying the reason why the output does not change. Fixing the issue requires a conceptual understanding of the task at hand, which is something I don't have.
I changed my code with the above formula, and now it can output different results. Thank you very much.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 29 Oct 2021

Commented:

on 29 Oct 2021

Community Treasure Hunt

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

Start Hunting!