필터 지우기
필터 지우기

Try to plot position vs time

조회 수: 1 (최근 30일)
Kiran Isapure
Kiran Isapure 2023년 1월 18일
편집: Neelanshu 2023년 11월 17일
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
Kiran Isapure
Kiran Isapure 2023년 1월 19일
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 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
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 :
Hope this helps,
Regards,
Neelanshu

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by