Alex
Alex

Reputation: 185

Matlab - Roots of polynomial with varying coefficients

Suppose p=[3,2,1]. Then, roots(p) gives the roots of 3x^2+2x+1.

What if the coefficients of a polynomial depend on a coefficient a? Like a polynomial 3x^2+2x+a. If I define p = @(a) [3,2,a] then roots(p) doesn't work. The error is

Undefined function 'isfinite' for input arguments of type 'function_handle'.

Is there a way of adjusting the roots function or do I have to retreat to fsolve to find the roots of a polynomial with varying coefficients?

Upvotes: 1

Views: 204

Answers (1)

juju89
juju89

Reputation: 549

You need to define a as a symbolic variable as follows:

syms a

Then

p = [3,2,a]
roots(p)

should work. I am using R2019b.

For more complex examples, if you know anything about the variable a (e.g. a is a real), you should let the roots() function know by defining the variable as a real:

syms a real

Upvotes: 3

Related Questions