Simulate a 2-input linear system with with ode45

Hello everyone,
I am new to ode45, I know how I should use ode45 to simulate a SISO system, but now I am trying to use it on a 2-input 2-output system.
The state space equations are as followed:
And here is my code trying to define the odefunction, the plot is not matching the result using lsim(), so it should not be correct.
t = [0 50]
x0 = [0 0 0 0 0];
[t,y] = ode45(@odefun,t,x0);
plot (t,y,'green');
grid on;
function dxdt = odefun(t,x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
%input u
u1 =10;
u2 =10;
dxdt = zeros(5,1);
dxdt(1) = -1/6*x1-1/3*x3+1/6*u1+1/3*u2;
dxdt(2) = x3;
dxdt(3) = 0.5*x1-0.5*x2-0.5*x3;
dxdt(4) = x1-x2-x3;
dxdt(5) = 0.5*u1 -0.5*x1;
end

 Accepted Answer

More like this:
tspan = [0 50];
x0 = [0; 0; 0];
u = [10; 10];
[t,x] = ode45(@(t,x) odefun(t,x,u),tspan,x0);
x = x';
My = [1 -1 -1; -1/2 0 0];
Ky = [0 0; 1/2 0];
y = My*x + Ky*u;
plot (t,y,'green');
grid on;
function dxdt = odefun(~,x,u)
M = [-1/6 0 -1/3; 0 0 1; 1/2 -1/2 -1/2];
K = [1/6 1/3; 0 0; 0 0];
dxdt = M*x + K*u;
end

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!