Simulate a 2-input linear system with with ode45

1 次查看(过去 30 天)
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

采纳的回答

Alan Stevens
Alan Stevens 2020-10-17
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by