Why this function does not run

Hi,
I am trying to build a customized function for pole placement in order to achieve the same result as place(). But I can not use place due to assignment restriction. So here is my code. But I try to run it on matlab 2025a and it does not work and it takes forever to run. Please help.
clc;clear;close all;
% first the linearized matrix is included below, it is from the project 1.
m = 1;
g = 9.81;
J_x = 0.01;
J_y = 0.01;
J_z = 0.02;
psi = 0;
% the sate is given by state = [x,y,z,vx,vy,vz,phi,theta,psi,p,q,r];
A = [
0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 g*sin(psi) g*cos(psi) 0 0 0 0;
0 0 0 0 0 0 -g*cos(psi) g*sin(psi) 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 1;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0];
B = [
0 0 0 0;
0 0 0 0;
0 0 0 0;
0 0 0 0;
0 0 0 0;
1/m 0 0 0;
0 0 0 0;
0 0 0 0;
0 0 0 0;
0 1/J_x 0 0;
0 0 1/J_y 0;
0 0 0 1/J_z];
C = zeros(4,12);
C(:,[1,2,3,9]) = eye(4); % here the observed outputs are x, y, z and psi
D = zeros(4,4); % here the d matrix is zero.
sys = ss(A,B,C,D); % this is the linearized system.
%%
poles = [-1.2 + 1.2*1j, -1.2 - 1.2*1j, -2.5, -3.0,...
-1.2 + 1.2*1j, -1.2 - 1.2*1j, -2.5, -3.0,...
-1.5 + 1.5*1j, -1.5 - 1.5*1j,...
-2+2*1j, -2-2*1j];
K = our_place(A,B,poles);
%% Local functions at the end of the script
function matrix = our_place(A,B,poles)
matrix = zeros(size(B,2),size(B,1));
matrix = sym("m",(size(matrix)));
s = sym("s");
S = s*eye(size(A));
left = det(S - (A-B*matrix)); %left hand side
right = poly2sym(poly(poles),s);
coefficientEquations = coeffs(expand(left - right), s, 'All') == 0;
solve(coefficientEquations, matrix(:));
end

2 Comments

Torsten
Torsten on 24 Nov 2026 at 20:31
Edited: Torsten on 24 Nov 2026 at 20:39
I cannot believe that you have to mimic the "place" function as part of your assignment because it's too hard to code. So: what is your assignment ?
At the moment, you try to solve a system of 12 polynomial equations (up to order 4) for 4*12 = 48 unknowns. This will not work.
Torsten
Torsten about 2 hours ago
Edited: Torsten about 2 hours ago
If you ask AI for MATLAB code without using the "place" function, it will give you alternative solutions which either use the "sylvester" function or Ackermann's formula. Try it.

Sign in to comment.

 Accepted Answer

The problem of the given system can be numerically solved using the built-in pole-placement function 'place()'. However, your custom pole-placement function doesn't work because you called solve(...) without capturing its output, so the symbolic unknowns in matrix were never replaced by the solved values.
%% Experiment: Simple case (expected result using built-in 'place')
Aa = [0 1;
0 0];
Bb = [0
1];
pp = [-1, -2]; % desired mathematical roots (Engineering poles)
Kk = place(Aa, Bb, pp)
Kk = 1×2
2.0000 3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
eig(Aa - Bb*Kk)
ans = 2×1
-1.0000 -2.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%% Test custom function
Kk = our_place(Aa, Bb, pp)
Kk = 
Kk = our_placeFixed(Aa, Bb, pp)
Kk = 
%% Local functions at the end of the script
function matrix = our_place(A, B, poles)
matrix = zeros(size(B,2),size(B,1));
matrix = sym("m",(size(matrix)));
s = sym("s");
S = s*eye(size(A));
left = det(S - (A-B*matrix)); % left hand side
right = poly2sym(poly(poles),s);
coefficientEquations = coeffs(expand(left - right), s, 'All') == 0;
solve(coefficientEquations, matrix(:));
end
function matrix = our_placeFixed(A, B, poles)
% Create symbolic unknowns matching size(K) = (size(B,2) x size(A,1))
matrix = sym("m", [size(B,2), size(A,1)]);
s = sym("s");
S = s*eye(size(A));
left = det(S - (A - B*matrix)); % characteristic poly of (A-BK)
right = poly2sym(poly(poles), s); % desired char poly
eqs = coeffs(expand(left - right), s, 'All') == 0;
vars = matrix(:);
sol = solve(eqs, vars); % solve returns struct with fields for each var
% Collect solutions in the same order as vars and reshape
vals = arrayfun(@(v) sol.(char(v)), vars, 'UniformOutput', false);
vals = vertcat(vals{:});
matrix = reshape(vals, size(matrix));
end

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Release

R2025a

Asked:

on 24 Nov 2026 at 19:46

Edited:

about 2 hours ago

Community Treasure Hunt

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

Start Hunting!