Try to plot position vs time

8 visualizaciones (últimos 30 días)
Kiran Isapure
Kiran Isapure el 18 de En. de 2023
Editada: nick el 17 de Nov. de 2023
I have bug in delerating phase in my code, I have attached two picture (one on paper is what i am trying to get)
Thanks
%% Intialization
vmax=300;
v0=0;
a=100;
y0=130;
ymax=10;
dt=0.02;
%% Create Position signal (y)
%Lead in 1 second at a constant position
t=dt:dt:1;
y(1:length(t))=y0;
v(1:length(t))=v0;
%% accelerating phase
i=length(t);
while v(i)<vmax
i=i+1;
t(i)=t(i-1)+dt;
v(i)=v(i-1)+a*dt;
y(i)=y(i-1)-v(i)*dt+1/2*a*dt.^2;
end
%% constant velocity phase phase
for i=1:10 %need to determine how long the costant velocity phase would need to be 10 to 12 secs
t(i)=t(i-1)+dt;
v(i)=vmax;
y(i)=y(i-1)-v(i)*dt;
%end
%% delerating phase
% To do list
A=-a;
i=length(t);
while v(i)>-30
i=i+1;
t(i)=t(i-1)+dt;
v(i)=v(i-1)+a*dt;
y(i)=y(i-1)-v(i)*dt+1/2*a*dt.^2;
end
yDeac=y(i)-y0;
v(k)=-30;
ideac=i;
  5 comentarios
Kiran Isapure
Kiran Isapure el 19 de En. de 2023
Yes, I continued to use the old varible.I made those changes, my decision are based on position, for acceleration phase my position varible should be y <ymax - number of degrees for deleration phase.
Should I need to use if statement after foor loop (acceleration phase).?
Jan
Jan el 20 de En. de 2023
This is strange:
t(i) = t(i-1) + dt;
v(i) = v(i-1) + a*dt;
y(i) = y(i-1) - v(i)*dt + 1/2*a*dt.^2;
The acceleration is considered twice. a/2*t^2 is the integrated velocity already.

Iniciar sesión para comentar.

Respuestas (1)

nick
nick el 17 de Nov. de 2023
Editada: nick el 17 de Nov. de 2023
Hi Kiran,
I understand that you are facing an issue with debugging the decelerating phase in your code and in obtaining the desired position vs time plot.
In the constant velocity phase there is array indexing error. MATLAB array indices must be positive integer, i.e. greater than or equal to 1. Also the following section of code rewrites the intital velocity which is incorrect. Furthermore, in the acceleration phase as well as the constant velocity phase, I have infered from the position vs time plot, the equation of motion for distance is incorrect.
%% accelerating phase
i=length(t);
while v(i)<vmax
i=i+1;
t(i)=t(i-1)+dt;
v(i)=v(i-1)+a*dt;
y(i)=y(i-1)-v(i)*dt+1/2*a*dt.^2;
end
%% constant velocity phase
for i=1:10 %need to determine how long the constant velocity phase would need to be 10 to 12 secs
t(i)=t(i-1)+dt;
v(i)=vmax;
y(i)=y(i-1)-v(i)*dt;
end
Instead, the code should look like,
%% accelerating phase
i=length(t);
while v(i)<vmax
i=i+1;
t(i)=t(i-1)+dt;
v(i)=v(i-1)+a*dt;
y(i)=y(i-1)+v(i)*dt+1/2*a*dt.^2;
end
%% constant velocity phase
for m = 1:10 %need to determine how long the constant velocity phase would need to be 10 to 12 secs
i=i+1;
t(i) = t(i-1)+dt;
v(i) = vmax;
y(i) = y(i-1)+v(i)*dt;
end
Also as @dpb mentioned the acceleration should be 'A' instead of '-a' so as to decelerate the object. Furthermore, like in the acceleration phase, in this phase also the equation of motion for distance is incorrect. I have attached sample code for the deceleration phase for your reference
%% decelerating phase
A=-a;
while v(i)>-30
i=i+1;
t(i)=t(i-1)+dt;
v(i)=v(i-1)+A*dt;
y(i)=y(i-1)+v(i)*dt+1/2*A*dt.^2;
end
I have obtained the distance vs time plotlike the desired result and attached the figure for your reference :
Hope this helps,
Regards,
Neelanshu

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by