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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by