How to count the number of iterations in a loop?

13 visualizaciones (últimos 30 días)
Andrea Mira
Andrea Mira el 19 de Mzo. de 2021
Comentada: Mathieu NOE el 24 de Mzo. de 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

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 19 de Mzo. de 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 comentarios
Andrea Mira
Andrea Mira el 24 de Mzo. de 2021
Thanks very much @Mathieu NOE
Mathieu NOE
Mathieu NOE el 24 de Mzo. de 2021
you're welcome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by