Try to plot position vs time
조회 수: 1 (최근 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!