problems with "if statement"

2 views (last 30 days)
Igor Braz Gonzaga
Igor Braz Gonzaga on 13 Mar 2022
Answered: Voss on 14 Mar 2022
Hi everyone. I am beginning in Matlab and I have a problem.
My code follows:
for i=1:Nped
for j=1:length(t)
dist(i,j)=vp(i)*t(j);
for k=1:NH
if (dist(i,j)<=Lenght)
Findiv(i,:)=G(i)+alfadin(1)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(1));
else
Findiv(i,:)=0
end
end
end
For some reason, the loop :
if (dist(i,j)<=Lenght)
.... is not been well executed. I ploted the graphic of dist(1,j) vs t (time) and I verified that for a distance dist(1, j ) > lenght (where lenght= 20 m), the non-zero expression give by
Findiv(i,:)=G(i)+alfadin(1)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(1))
is not calculated (as showed in the second graph).
Does anyone could help me to solve this problem?
Thanks a lot =)
  4 Comments
Igor Braz Gonzaga
Igor Braz Gonzaga on 13 Mar 2022
I'm sorry, I copied the test code. The correct follows:
for i=1:Nped
Findiv=G(i)+alfadin(1)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(1));
for j=1:length(t)
dist(i,j)=vp(i)*t(j);
for k=1:NH
if (dist(i,j)<=20) %&(t(j)>=Tentr(i))
Findiv(i,:)=G(i)+alfadin(k)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(k));
else
Findiv(i,:)=0;
end
end
%Ft(:)=Ft(:)+Findiv(i,:)
end
Jan
Jan on 14 Mar 2022
In this version "phase(k)" appears. In the version you post in the comments below, it is "pahse(1)". This optimizes the cofusion fro the readers. We see 3 different codes now and all of them do, what they are doing. You are expecting something else, but we cannot guess why.
Use the debugger to examine whats going on. Set a breakpoint in the first line, then step through the code line by line. Of course, Matlab does exactly what it is instructed to do. The IF condition does work properly, so the only problem is that you expect something else.
The purpose if the "for k=1:NH" loop is still not clear and inside it Findiv is overwritten repeatedly. This cannot be useful.

Sign in to comment.

Accepted Answer

Jan
Jan on 13 Mar 2022
Edited: Jan on 13 Mar 2022
The if command is not a loop.
What is the purpose if the "for k" loop? Its body does not depend on k, so you can omit this loop. Instead of setting the value of Finfiv explicitly to 0, you can pre-allocate also:
Findiv = zeros(Nped, 1);
for i = 1:Nped
tmp = G(i) + alfadin(1) * G(i) * ...
sin(2 * pi * i * fp(i) * (t - Tentr(i)) - phase(1));
for j = 1:length(t)
dist(i,j) = vp(i) * t(j);
if dist(i, j) <= Lenght
Findiv(i) = tmp;
break;
end
end
end
I've moved the calculation of G(i) + alfadin(1) *... out of the inner loop, because it does not depend on j and calculating it repeatedly is a waste of time. Therefore I've inserted a break also to leave the inner loop soon.
Maybe this is the problem: Findiv(i) is overwritten in each iteration of the loop over j, so it depends on the last value vp(i)*t(end) only.
  8 Comments
Torsten
Torsten on 14 Mar 2022
Make that MATLAB recognizes t - otherwise the graphs will make no sense.
Still, the k-loop is superfluous - k is never referenced.
Image Analyst
Image Analyst on 14 Mar 2022
You say that's what yhou got but that's not true. If you did get that, then you'd have gotten the error about t not being defined and you never would have gotten your plot. I'm really about to quit here. Either post the full code or I'm outta here. Perhaps Torsten will still be kind enough to help you.

Sign in to comment.

More Answers (1)

Voss
Voss on 14 Mar 2022
A couple of people have pointed out that the "for k=1:NH" loop is not necessary, but since it is still part of the code, can you check the value of NH?
If NH is less than 1 or NH is the empty array, then the "for k=1:NH" loop will execute zero times, leading to the if statement not being checked at all and Findiv not being calculated (but dist will be calculated because that is done outside the "for k=1:NH" loop).
This possibility is consistent with your observations of the values of dist and Findiv, if Findiv is pre-allocated with zeros.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!