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.

Réponses (1)

Walter Roberson
Walter Roberson le 11 Fév 2023

0 votes

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.

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by