How to count the number of iterations in a loop?

12 ビュー (過去 30 日間)
Andrea Mira
Andrea Mira 2021 年 3 月 19 日
コメント済み: Mathieu NOE 2021 年 3 月 24 日
Hi everybody,
I'm new in MATLAB. I would like to count the number of iterations in the following loop while distance between particles are lower than a specified minimum (i.e 5NM)
Thanks in advance!
---------------------------------------------------------------------------------
clc; clear all; close all
%Initial and final points of particles runways
%Particle 1
a1=0; a2=40;
b1=100; b2=40
%Particle 2
c1=45; c2=0;
d1=45; d2=100;
%particles speed (NM/seconds)
va=520/3600;
vb=520/3600;
%Iteration interval
At=1;
%Initial positions
xa(1)=a1; ya(1)=a2;
xb(1)=c1; yb(1)=c2;
%Velocity projection
Vxa= va*cos(0);
Vya=va*0;
Vxb=vb*cos(pi/2);
Vyb=vb*sin(pi/2);
%Loop for particles porsitions
for t=2:1000
%Particle A
xa(t)=xa(t-1)+Vxa*At;
ya(t)=ya(t-1)+Vya*At;
%Particle B
xb(t)=xb(t-1)+Vxb*At;
yb(t)=yb(t-1)+Vyb*At;
Dist = 5;
aux(1,t)=norm([xa(t) ya(t)] - [xb(t) yb(t)]);
% if Dist>aux
% % count the number of iterations (time in seconds) while distance between both particles is less than 5
% end
end
d=sqrt((xa-xb).^2+(ya-yb).^2); %Distance between particles
plot(xa,ya,xb,yb)
axis([0, 100, 0, 100]);
grid on

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 3 月 19 日
hello
at the very beginnin gof your ode you should initilalize a counter
k = 0;
then in the loop , you do :
if Dist>aux
% count the number of iterations (time in seconds)
% while distance between both particles is less than 5
k = k + 1;
end
  4 件のコメント
Andrea Mira
Andrea Mira 2021 年 3 月 24 日
Thanks very much @Mathieu NOE
Mathieu NOE
Mathieu NOE 2021 年 3 月 24 日
you're welcome

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by