Using symbolic syms inside a Simulink Function — Error: Symbolic not allowed

55 views (last 30 days)
Hello everyone,
I am working with MATLAB/Simulink and trying to implement a time-varying LQR (TV-LQR).
At each simulation step, I want to:
  1. Take the desired state x_desired
  2. Linearize the nonlinear model around this state to compute matrices A and B
  3. Compute the LQR gain K
  4. Apply the control lawu=udesiredK(xxdesired)u = u_{\text{desired}} - K (x - x_{\text{desired}})u=udesiredK(xxdesired)
The problem is that Simulink gives an error saying that symbolic variables (syms) are not allowed inside a Simulink Function.
How can I linearize my model at each step without using symbolic variables in Simulink?
Is there a recommended way to do numerical linearization at runtime?
Thanks!

Accepted Answer

Sam Chak
Sam Chak on 25 Nov 2025 at 8:48
Before modeling the control system in Simulink, I advise that you first understand the underlying mathematical design concepts, as the lqr() function is executed in MATLAB. I assume you will refer to textbooks to comprehend how the LQR algorithm calculates the control gain matrix by solving the Riccati differential equation. Thus, I won't elaborate further on that topic.
Many students linearize the nonlinear system and straightforwardly apply the control gain matrix computed by the LQR algorithm. However, they are often unable to achieve zero steady-state error because they tend to unknowingly neglect the effects of state-dependent disturbance, which is the byproduct of the mathematical linearization.
Here is a simple example for the control design.
Step 1: Get the mathemtical model of the inverted pendulum (an unstable nonlinear system):
where u is the controller.
Step 2: Linearize the nonlinearity:
The equation for the linearization of the function at is given by
.
Step 3: Express the nonlinear system in the linearized form for conrol design purposes:
where is the state-dependent disturbance.
If we design the controller as by introducing an auxiliary control variable μ, the linearized system becomes
or in the state-space representation
.
where is a state-dependent state matrix whose values change based on the system's current state, particularly .
Step 4: Obtain the feedback control gain matrix K using the LQR algorithm:
function dx = linearInvertedPendulum(t, x)
% definition
a = x(1);
% state-dependent disturbance
d = sin(a) - a*cos(a);
% related to LQR design
A = [0, 1; cos(a), 0]; % state matrix
B = [0; 1]; % input matrix
Q = 1e2*eye(2); % state weighting factor
R = 1; % input weighting factor
% state-dependent feedback control gain matrix
K = lqr(A, B, Q, R);
% auxiliary control variable
mu = - K*[x(1); x(2)]; % mu = - K*x
% original controller
u = mu - d; % cancel out the effects of state-dependent disturbance
% original dynamics (DO NOT USE THE LINEARIZED FORM!)
dx(1) = x(2);
dx(2) = sin(x(1)) + u;
% system
dx = [dx(1);
dx(2)];
end
Step 5: Solve using ode45 and plot the result:
[t, x] = ode45(@linearInvertedPendulum, [0 10], [1, 0]);
plot(t, x(:,1)), grid on
xlabel('Time, (s)')
ylabel('Angle, (rad)')
title('Pendulum''s Angle, $\theta$', 'interpreter', 'latex')

More Answers (1)

Walter Roberson
Walter Roberson on 25 Nov 2025 at 6:26
You cannot mix symbolic variables and Control System Toolbox or Simulink Control Design https://www.mathworks.com/help/slcontrol/ug/linearize.html .
I am not aware of anything in the Symbolic Toolbox that would help to linearize models. (Though it is true that you can do some steps using the laplace transform of transform functions.)
In https://www.mathworks.com/matlabcentral/answers/305339-how-to-create-a-transfer-function-with-gain-k#comment_395202 I discussed the closest that you can get to symbolic variables using Simulink models (it is not very close.)

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!