How to count the number of iterations in a loop?

13 views (last 30 days)
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

Accepted Answer

Mathieu NOE
Mathieu NOE on 19 Mar 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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!