How to integrate a control system by ode45 with PID control input ?

조회 수: 33 (최근 30일)
Chuguang Pan
Chuguang Pan 2025년 11월 20일 14:07
댓글: Chuguang Pan 2025년 11월 21일 2:15
I want to simulate a control system using ode45 function. The system model can be expressed as , where the control input u is the PID control which can be expressed as . The is the desired state. My question is how to integrate the error in the ode model as illustrated in the following code example, since we only obtain the current time state X when the f function is called
function dX = f(t,X,P)
% ...
% ...
u = P.Kp * (P.Xd - X) + P.Ki * (P.Xd - X) + ... ; % How to integrate the error Xd - X ?
end

채택된 답변

Sam Chak
Sam Chak 2025년 11월 20일 15:25
Perhaps you can try this approach.
G = tf(1, [1, 0, 0]);
opt = pidtuneOptions('DesignFocus', 'disturbance-rejection');
wc = 5;
C = pidtune(G, 'PID', wc, opt);
% Parameters
P.Xd = 3; % desired state for X(1)
P.Kp = C.Kp; % proportional gain
P.Ki = C.Ki; % integral gain
P.Kd = C.Kd; % derivative gain
% ODE function
function dX = f(t, X, P)
% error
e = P.Xd - X(1);
% PID controller
u = P.Kp*e + P.Ki*X(3) - P.Kd*X(2);
% disturbance
d = 1;
% original 2nd-order system
dX(1) = X(2);
dX(2) = d + u;
% X(3) is ∫ e dt
dX(3) = e;
% system dynamics
dX = [dX(1)
dX(2)
dX(3)];
end
% Solve and plot
[t, X] = ode45(@(t, X) f(t, X, P), [0, 10], zeros(3, 1));
plot(t, X(:,1)), grid on
title('Time response for state x_{1}')
xlabel('t')
ylabel('x_{1}')

추가 답변 (1개)

Torsten
Torsten 2025년 11월 20일 14:19
Add a second differential equation
dV/dt = P.Xd - X
with initial condition
V(0) = 0
Then
V(t) = integral_{0}^{t} (P.Xd - X) dt

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by