How to count the number of iterations in a loop?

12 Ansichten (letzte 30 Tage)
Andrea Mira
Andrea Mira am 19 Mär. 2021
Kommentiert: Mathieu NOE am 24 Mär. 2021
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

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 19 Mär. 2021
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 Kommentare
Andrea Mira
Andrea Mira am 24 Mär. 2021
Thanks very much @Mathieu NOE
Mathieu NOE
Mathieu NOE am 24 Mär. 2021
you're welcome

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by