LQR Controller for Nonlinear System Tracking with Time-Varying Linearization

33 views (last 30 days)
Hello everyone,
I'm trying to implement an LQR controller for a nonlinear system to track a reference trajectory. I need to linearize around each point of the trajectory and compute time-varying LQR gains.
My current problem is how to implement the following:
  • From a nonlinear system: xdot = f(x, u)
  • I need to linearize around each reference point (x_ref(t), u_ref(t)) to get A(t) and B(t)
  • Compute LQR gains K(t) at each time step
  • Apply the control input
My MATLAB code is as follows:
matlab
Q = zeros(size(A));
R = eye(size(B,2));
[K, S, e] = lqr(A, B, Q, R);
A_cl = A - B*K;
B_cl = zeros(size(B));
C_cl = C;
D_cl = D;
sys_cl = ss(A_cl, B_cl, C_cl, D_cl);
[y_cl_dev, t_sim, x_cl_dev] = initial(sys_cl, delta_x0, t);
x_cl_phys = x_cl_dev + repmat(X_op.', length(t_sim), 1);
y_cl_phys = y_cl_dev + repmat((C*X_op).', length(t_sim), 1);
My Question:
How can I use ODE45 to, at each point, linearize the system to get A and B, compute K, apply the control input, and then obtain xdot and y?
How convert the
initial
function to a loop-based simulation.
thanks.
  2 Comments
Sam Chak
Sam Chak on 30 Nov 2025 at 14:10
According to the documentation for the initial() function, this function can only be used with the following types of state-space models: (1) numeric state-space ss models, (2) uncertain linear time-invariant models, such as genss or uss models, (3) sparse state-space models, such as sparss and mechss models, (4) linear time-varying (ltvss) and (5) linear parameter-varying (lpvss) models.
If your nonlinear system is time-invariant and you wish to linearize it at each time step to obtain the state-space model, it may be computationally inefficient, as the same generic Jacobian matrices will be computed repeatedly. Different state-space models will result when you substitute the instantaneous operating points into the Jacobian matrices. Could you compute the Jacobian matrices only once?
To enable the initial() function to work, you can attempt to convert the time-varying nonlinear system into an ltvss model, or the parameter-varying nonlinear system into an lpvss model, though not every nonlinear system can be expressed as an ltvss or lpvss model.
boutegui
boutegui on 30 Nov 2025 at 23:11
Hello sir,
I mean: if Simulink runs from t = 0 to t = 5 seconds with a step of 0.01, how can I do the same using ode45 over the entire timeline using a for loop?
Thanks.

Sign in to comment.

Answers (1)

Sam Chak
Sam Chak on 1 Dec 2025 at 4:45
So you want to run ode45 in a loop like this: at the start of each time step, inject the linearized state-space model into the ODE function so the LQR computes the gains and applies the control to the original nonlinear system?
The code snippet below skips the linearization and LQR computation (assuming you already know them). It only shows the ode45 looping.
%% Direct approach
% [t, y] = ode45(@(t, x) - 2*x, [0 5], 1); % dx/dt = - 2*x; y = x;
% plot(t, y), grid on
%% Looping approach
tspan = 0:0.01:5; % simulation duration
y0 = zeros(1, numel(tspan));
y0(1) = 1; % initial value
hold on
for i = 1:length(tspan)-1
[t, y] = ode45(@(t, x) - 2*x, [tspan(i), tspan(i+1)], y0(i));
y0(i+1) = y(end);
plot(t, y, 'color', "#80B3FF", 'linewidth', 1.5)
end
hold off
grid on
xlabel('t')
ylabel('y')
title('Output')

Categories

Find more on Matrix Computations in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!