Effacer les filtres
Effacer les filtres

Try to plot position vs time

1 vue (au cours des 30 derniers jours)
Kiran Isapure
Kiran Isapure le 18 Jan 2023
Modifié(e) : Neelanshu le 17 Nov 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 commentaires
Kiran Isapure
Kiran Isapure le 19 Jan 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 le 20 Jan 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.

Connectez-vous pour commenter.

Réponses (1)

Neelanshu
Neelanshu le 17 Nov 2023
Modifié(e) : Neelanshu le 17 Nov 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

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by