MATLAB: How to find the ODE for a statespace

ode45 statespace

I have this code:
for k = 1:3
Vx = Vx + dVx;
dfd = 4; df = dfd*pi/180;
a1 = 2*(Caf+Car)/Vx;
a2 = (M*Vx^2+2*L1*Caf-2*L2*Car)/Vx;
a3 = 2*(L1*Caf-L2*Car)/Vx;
a4 = 2*(L1^2*Caf+L2^2*Car)/Vx;
K = [a1,a2;a3,a4];
b = [2*Caf;2*L1*Caf];
A = -Minv*K; B = Minv*b;
C = [1,0;0,1]; D = [0];
sys = ss(A,B,C,D); [y,t] = step(df*sys);
end
And I am trying to find the ode for the state space to solve it.
I tried doing:
ode45(@(t,y) sys, 0:1:100, [0; 0])
But I am getting this error:
Error using odearguments (line 113)
Inputs must be floats, namely single or double.
I am not sure how to find it. Again I am attempting to find the ode45 for the 2 equations I have in the state space.

Best Answer

In Matlab, state-space model cannot be directly converted into a system of ODE, but you don't need to convert it. Differential equations for an LTI state-space model is defined by the following matrix equation,
so you can write your own ode function to use with ode45 like this
function dxdt = odeFun(t,x,A,B)
u = 1; % for step input
dxdt = A*x+B; % simply write the equation
end
and then call the oder 45 like this
[t x] = ode45(@(t,x) odeFun(t,x,A,B), 0:1:100, [0; 0]); % here A and B are the name of your state space matrices
y = C*x'; % D = 0;
Related Question