How to simulate the state-space model

54 vues (au cours des 30 derniers jours)
Hein zaw
Hein zaw le 23 Oct 2023
Commenté : munachi le 25 Nov 2024
Hello,
How to do the follow stace-space model in matlab or simulink. I only found the one input and output example.
thanks.

Réponse acceptée

Sam Chak
Sam Chak le 23 Oct 2023
In MATLAB, you can use the ODE solver approach. If the system is LTI and you have the Control System Toolbox installed, you can also use the lsim() command. However, I generally advise students to learn the ODE solver approach because it provides a more versatile skill that can be applied to both linear and nonlinear state-space systems, avoiding the need to learn something new when dealing with the latter.
% Call ode45 solver
tspan = linspace(0, 10, 1001);
x0 = [0, 0];
[t, x] = ode45(@odefcn, tspan, x0);
% Pre-allocate for the system output signal y
y = zeros(1, numel(t));
% Use for-loop to call odefcn() and compute y
for i = 1:numel(t)
[~, y(i)] = odefcn(t(i), x(i,:).');
end
figure
plot(t, x, 'linewidth', 1.5), hold on
plot(t, y, 'linewidth', 1.5), hold off, grid on
xlabel('Time'), legend('x_1', 'x_2', 'y', 'location', 'best')
% Create a function that returns two outputs (math): dxdt and y
function [dxdt, y] = odefcn(t, x)
% Inputs
u1 = heaviside(t);
u2 = 0.1*sin(2*pi/10*t);
u = [u1;
u2];
% Matrix definitions
A = [ 0 1; % state matrix
-2 -3];
B = [ 0 0; % input matrix
1 1];
C = [ 3 1]; % output matrix
D = 0*C*B; % direct matrix
% State-space system
dxdt = A*x + B*u; % state equation
y = C*x + D*u; % output equation (system)
end
  2 commentaires
Hein zaw
Hein zaw le 24 Oct 2023
Thank you, I will try.
munachi
munachi le 25 Nov 2024
@Sam Chak is there a way I can reach out to you. I have some problem with matlab and I think you might be my savior.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Trimming and Linearization dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by