简体   繁体   中英

Finding the roots of a polynomial defined as a function handle in matlab

I need to be able to find the roots of a couple of polynomials that are almost characteristic functions, but not quite (rather than an eigenvalue, it's more like an eigen-block matrix). The function is defined as a function handle because I don't have analytic expressions for the coefficients on the equation (I could presumably find them but the algebra in my model is already quite messy).

The equations are

charA = @(k1,k2) det([k1*eye(N),zeros(N);zeros(N),k2*eye(N)]*Amatrix - eye(2*N));
charB = @(k1,k2) det([k1*eye(N),zeros(N);zeros(N),k2*eye(N)]*Bmatrix - eye(2*N));

and I need to find all of the roots of each one, since the solution of the system is the pair (k1,k2) that satisfies charA(k1,k2)=0 and charB(k1,k2)=0 (at the moment I'm just trusting that the derivations of these matrices are such that such a solution exists, but for the purpose of this question - finding all of the roots of a polynomial defined in this sort of way - this is unimportant).

Is there any way I can take this function handle and turn it into a matrix of coefficients, or is there a solver for polynomials defined as function handles in Matlab? If it changes anything, the matrices aren't massive but they are 84x84, that is, N=42.

As said Daniel

  • use symbolic math toolbox to build the polynomials
  • Then use solve() to find the roots

The sample code is as follows with N = 2

rng(0)
%Unknowns
syms k1 k2
N = 2;
Amatrix = rand(2*N);
Bmatrix = rand(2*N);

charA = det([k1*eye(N),zeros(N);zeros(N),k2*eye(N)]*Amatrix - eye(2*N));
charB = det([k1*eye(N),zeros(N);zeros(N),k2*eye(N)]*Bmatrix - eye(2*N));

% solver 
solution = solve(charA == 0, charB == 0);

% Convert syms to numeric, specifying precision as 3 
k1_solution = vpa(solution.k1, 3)
k2_solution = vpa(solution.k2, 3)

% Only real solution
k1_solution_real = vpa(k1_solution(k1_solution == real(k1_solution)), 3)
k2_solution_real = vpa(k2_solution(k2_solution == real(k2_solution)), 3)

Solution

  • All
k1_solution =

          0.475
          -2.52
         0.0161
 - 1.58 + 1.79i
 - 1.6 - 1.79i
 - 2.0 - 0.863i
 - 2.0 + 0.865i
           11.2


k2_solution =

            0.345
           -0.869
            0.946
 - 1.37 + 0.0219i
 - 1.37 - 0.0219i
     1.69 + 3.24i
     1.69 - 3.24i
            -5.65
  • Real only
k1_solution_real =

  0.475
  -2.52
 0.0161
   11.2


k2_solution_real =

  0.345
 -0.869
  0.946
  -5.65
  • First root solution
k1 = 0.475 and k2 = 0.345

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM