how to plot speed time
이전 댓글 표시
hi
I've tried to make code that calculates and plots velocity according to an increasing linear function
f=@(t,dphi) ((t<t(1))*dphi(1)+...
((t>t(1))&(t<t(2))).*((dphi(2)-dphi(1))/(t(2)-t(1))*(time-t(1))+dphi(1)*ones(size(t)))+...
(t>t(2))*dphi(2).*ones(size(t)));
[t,dphi]=ode45(@(t,dphi)f,[0,6],[0,pi/30*4.7e4]);
if t<=t(1)
phi= dphi(1)*t;
d_phi= dphi(1);
dd_phi =0;
elseif t>=t1&&t<=t(2)
phi = (dphi(2)-dphi(1))/(t(2)-t(1))*(t-t(1))^2/2+dphi(1)*t;
d_phi = (dphi(2)-dphi(1))/(t(2)-t(1))*(t-t(1))+dphi(1);
dd_phi = (dphi(2)-dphi(1))/(t(2)-t(1));
elseif t>=t(2)
phi= dphi(2)*t-(dphi(2)-dphi(1))*(t(1)+t(2))/2;
d_phi= dphi(2);
dd_phi =0;
end
When running I get the following error
@(T,DPHI)F returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by
Any Ideas, on how I should do this ?
Thanks in advance for your help.
답변 (1개)
Walter Roberson
2023년 2월 11일
f=@(t,dphi) ((t<t(1))*dphi(1)+...
((t>t(1))&(t<t(2))).*((dphi(2)-dphi(1))/(t(2)-t(1))*(time-t(1))+dphi(1)*ones(size(t)))+...
(t>t(2))*dphi(2).*ones(size(t)));
In all of the ode* functions, the first parameter passed to the ode function is always scalar, so ones(size(t)) would always be just a scalar 1 . Furthermore, t(2) would not exist.
[t,dphi]=ode45(@(t,dphi)f,[0,6],[0,pi/30*4.7e4]);
That code does not tell ode45 to invoke the function handle f passing in t and dphi: it tells ode45 that that given t and dphi, the result to be returned is the function handle f itself . Use
[t,dphi]=ode45(f,[0,6],[0,pi/30*4.7e4]);
Note that except in cases where the function immediately goes badly discontinuous and cannot be stepped at time 0, then ode45 is going to return a column vector of times in t.
if t<=t(1)
that would be a vector comparison, which would be considered true only of all elements in t were <= t(1) . None of your if or elseif conditions are going to match.
elseif t>=t1&&t<=t(2)
t1 is not defined.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!