Can I use "place" function to adaptively update controller gain K when running model in Simulink?

I am running my model in Simulink, then send some information to the Matlab function which is used for updating controller gain K. In Matlab function, I call another m file from Matlab environment which has "place" function inside. The error is "place" is not supported for code generation.
How can I solve that problem?

Answers (1)

If the place() function is not supported for code generation, you will need to program the computation manually. After all, the essence of the pole placement technique, in mathematical terms, is fundamentally about solving algebraic equations.
%% system
A = [0, 1 % state matrix
-6, -5];
B = [0 % input matrix
1];
eA = eig(A) % eigenvalues of open-loop system
eA = 2×1
-2.0000 -3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% Manual Pole Placement
e = [-5, -7]; % desired closed-loop eigenvalues
p2 = poly(e) % desired characteristic polynomial
p2 = 1×3
1 12 35
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
k1 = p2(3) + A(2,1); % gain 1 (proportional)
k2 = p2(2) + A(2,2); % gain 2 (derivative)
K = [k1, k2] % control gain matrix
K = 1×2
29 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% verify the result with computational pole placement
K = place(A, B, e)
K = 1×2
29.0000 7.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Aa = A - B*K
Aa = 2×2
0 1.0000 -35.0000 -12.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
eig(Aa) % eigenvalues of closed-loop system
ans = 2×1
-5.0000 -7.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

5 Comments

Thanks for your comment. I agree, but the place() function implements a robust pole-placement algorithm that usually yields better results than the conventional approach. As you suggested, one workaround is to code the computation manually; however, re-implementing the robust pole-placement method this way is time-consuming.
If you can call MATLAB from your desired programming language, then you can declare the place() command as an extrinsic function using coder.extrinsic. Otherwise, consider using formulas, as most linear algebra functions are supported for code generation.
%% system
A = [0, 1 % state matrix
-6, -5];
B = [0 % input matrix
1];
eA = eig(A) % eigenvalues of open-loop system
eA = 2×1
-2.0000 -3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% Manual Pole Placement
e = [-5, -7]; % desired closed-loop eigenvalues
p2 = poly(e) % desired characteristic polynomial
p2 = 1×3
1 12 35
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% control gain matrix
Co = [B, A*B];
iCo = inv(Co);
K = [0, 1]*iCo*(p2(1)*A^2 + p2(2)*A + p2(3)*eye(2))
K = 1×2
29 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% verify the result with computational pole placement
K = place(A, B, e)
K = 1×2
29.0000 7.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Aa = A - B*K
Aa = 2×2
0 1.0000 -35.0000 -12.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
eig(Aa) % eigenvalues of closed-loop system
ans = 2×1
-5.0000 -7.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
again, program "place" function manually is more complicated than I thought as it is robust pole placement algorithm.
I tried to use coder.extrinsic before, however, the simulation time is much more slower. :(

Please consult with the technical support team. They should be able to help you.

Sign in to comment.

Categories

Products

Release

R2024b

Asked:

on 14 May 2025

Commented:

on 17 May 2025

Community Treasure Hunt

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

Start Hunting!