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 件のコメント

Image Analyst
Image Analyst 2022 年 3 月 13 日
What is Lenght, lenght, and length? Those are three different spellings and capitalizations and MATLAB is sensitive to mispellings and is case sensitive. Also, where is your plotting code?
Torsten
Torsten 2022 年 3 月 13 日
Where do the loop variables "k" and "j" come into play within the k-loop ?
As written, the k-loop is superfluous.
Igor Braz Gonzaga
Igor Braz Gonzaga 2022 年 3 月 13 日
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 2022 年 3 月 14 日
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.

サインインしてコメントする。

 採用された回答

Jan
Jan 2022 年 3 月 13 日
編集済み: Jan 2022 年 3 月 13 日

0 投票

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 件のコメント

Torsten
Torsten 2022 年 3 月 13 日
編集済み: Torsten 2022 年 3 月 13 日
tmp is a vector because of the t-array in it.
Igor Braz Gonzaga
Igor Braz Gonzaga 2022 年 3 月 13 日
Hi Jan, I'm sorry. I coppied I test, the correct code 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)
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
end
Image Analyst
Image Analyst 2022 年 3 月 13 日
After you read this link
Tell us the missing variables (like Nped, alfadin, etc.) so we can actually run your code. And, again, show us the code you used to plot those two figures you posted.
Igor Braz Gonzaga
Igor Braz Gonzaga 2022 年 3 月 13 日
Nped is an integer = 6 (number of pedestrians);
alfadin is a vector with two components: alfadin(1)=0.4 and alfadin(2)=0.1;
Lenght=20m is the controller variable that control Findv, if L<= 20m, than Findiv is calculated, else, Findiv=0;
dist( Nped, Lenght(t)) = is the distance. Calculated by multiplying the velocity of each person "v(i)" by the time " t ";
To plot the graphs I used the lines on the end of the code
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
plot(t,Findiv(1,:));
xlabel('Time');
ylabel('Findiv');
Image Analyst
Image Analyst 2022 年 3 月 13 日
Nped = 6;
alfadin(1)=0.4 ;
alfadin(2)=0.1;
Lenght=20;
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
plot(t,Findiv(1,:));
xlabel('Time');
ylabel('Findiv');
end
Unrecognized function or variable 't'.
So is that what you get?
Igor Braz Gonzaga
Igor Braz Gonzaga 2022 年 3 月 14 日
Yes. But something is wrong. Take a look what happens when I plot Findiv(1, : ):
I've plotted the graph of dist( 1, : ) and it works well (it means that there is a limite, in time, between values smaller and higher than the defined parameter Lenght, as indicated in the law of the IF condition).
So, I dont understand why Findv (1 , :) is zero for all times. It is like the condition "IF" just works for the ELSE block.
Torsten
Torsten 2022 年 3 月 14 日
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 2022 年 3 月 14 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2022 年 3 月 14 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2019b

質問済み:

2022 年 3 月 13 日

回答済み:

2022 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by