Try to plot position vs time
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2023-1-20
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.
回答(1 个)
Neelanshu
2023-11-17
编辑:Neelanshu
2023-11-17
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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1542217/image.png)
Hope this helps,
Regards,
Neelanshu
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!