Home > Software design >  Finding roots of a polynomial without writing it in the form of a matrix
Finding roots of a polynomial without writing it in the form of a matrix

Time:11-10

Is there any method to find the root of a polynomial, not in matrix form, in MATLAB?

I know, to find roots of a polynomial (say, p(x) = x^.2 - 4), I should do the following:

p = [1 0 -4];
r = roots(p)

What I wanted to know if there is some way to find the root of a function (say p(x) = x^.2 - 4) already present in polynomial form (not in matrix form) in my matlab code? Like anything similar to r = roots(p(x)) (this doesn't work, ofc.) Thanks!

CodePudding user response:

Root is good

First of all the solution using roots is probably the one that will give you the most accurate and fastest results if you are indeed working with polynomials. I will acknowledge that it might be an issue if your function is not a polynomial.

Finding the roots of a function

If you don't want to use roots that means you will probably have to represent your polynomial as an anonymous function. Then you can use any root-finding algorithm on that function. Wikipedia has a few of them listed. What is tricky is that in general they don't guarantee that they will find on root, let alone all of them. So you might need as much prior information on your function as you can.

In matlab you can use fzero. This has the default that it only finds one zero and that it will only find zeros where the function changes sign (it wouldn't work on p(x) = x² for example). This is how you would implement it:

p = @(x) x^.2 - 4;  % Define your polynomial as an anonymous function

x0 = 12;  % Initial guess for the zero

% Find a root
fzero(p, x0)
>>> ans = 2

% Now with a different initial guess for a different solution
x0 = -12;
fzero(p, x0)
>>> ans = -2

As you can see this works only if you want to find a root and don't care which one it is.

Problem

The issue is that you polynomials with integer or rational coefficients have a way of finding the roots by using square-free factorization. Yet you can only apply that if you have some way of storing and getting those coefficients in matlab. The anonymous functions don't allow you to do that. That's why roots works with a matrix and not an anonymous function.

  • Related