MATLAB: Using roots with vectorization of a polynomial vs. the function it’s self

MATLABpolynomialsrootsvectorization

x=-100:100
y=5*x.^5+4*x.^4+3*x.^3+2*x.^2+x
r = roots(y)
y1 = [5 4 3 2 1]
r1 = roots(y1)
why do you recieve more roots in the output when you run the roots with a function versus running it with a vectorized version of the coefficients of the function?

Best Answer

Look at what the roots function is documented to do.
r = roots(p) returns the roots of the polynomial represented by p as a column vector. Input p is a vector containing n+1 polynomial coefficients, starting with the coefficient of xn.
It does not accept a vector of values of your function and find the zeros of the function that takes those values. In other words, since x has 201 elements so does y and so r is the roots of a 200th degree polynomial not the 5th degree polynomial you evaluated to compute y. Zero crossings in a vector is better done by comparing the sign of the elements in that vector, or perhaps interpolation and fzero.
On the other hand, y1 does not represent the polynomial you evaluated to compute y. You're missing the constant term, which has to be present in the coefficient vector even if it's 0. But it's closer.
Related Question