polynomial in matlab roots

Hey ,I have a matrix
X= 0 0,142857142857143 0,285714285714286 0,428571428571429 0,571428571428571 0,714285714285714 0,857142857142857 1
and a matrix
Y=185654
189978
189202
192877
196255
200642
201231
206046
so I created the polynomial by p=polyfit(X,Y,7) and I was asked to find the real solutions of this so I used r=roots(p) and all i get are imaginary numbers. Why?

1 commentaire

Roger Stafford
Roger Stafford le 6 Jan 2016
It is not clear what "real solutions" you are trying to find. Solutions to what? The expression "roots(p)" would give you the points where p has a zero value and presumably there are none that are real-valued.
Since you have used degree 7 with 8 points, your polynomial p should match the values of Y at the points X precisely except for tiny round-off errors. Is that the "solution" you are seeking? In that case you already have it.

Connectez-vous pour commenter.

Réponses (2)

Walter Roberson
Walter Roberson le 6 Jan 2016

0 votes

Polynomials with an odd number of roots always enter through one side of the Y axis and exit through the other side of the Y axis (because -infinity to an odd power is still negative infinity, opposite sign of +infinity to an odd power, whereas for even number of roots, -infinity to an even power is positive infinity same as +infinity to an even power so with an even number of roots the graph will start and end on the same side.)
The graph plot(X,Y) enters from the lower left so it has to exit through the upper right. It does not cross 0 anywhere in the range of values so there are no internal real roots. The first value is positive. Therefore there must be a single real root that is to the lower left of the graph at an X smaller than any given value.
There would have to be some visible zero crossings within the defined range for there to be multiple real roots.
John D'Errico
John D'Errico le 6 Jan 2016
Modifié(e) : John D'Errico le 6 Jan 2016

0 votes

A 7th degree polynomial (with real coefficients) has at least ONE real root.
p = polyfit(X,Y',7);
roots(p)
ans =
1.0951 + 0.20415i
1.0951 - 0.20415i
0.63639 + 0.47627i
0.63639 - 0.47627i
0.10224 + 0.36777i
0.10224 - 0.36777i
-0.15631 + 0i
Note that the last root is NOT complex. It has a zero imaginary part. It is the only real root, at x=-0.15631...

6 commentaires

Roger Stafford
Roger Stafford le 7 Jan 2016
John, there is a potential weak point in this reasoning, though apparently it doesn't apply with the given data in this problem. Just because a '7' is specified in the third argument of 'polyfit' does not mean that it must come up with a seventh degree polynomial. It could be a polynomial of smaller degree if its upper coefficient or coefficients turn out to be zero. For an extreme example, if all eight values in Y were equal and nonzero, 'polyfit' ought to come up with a zero degree polynomial which obviously would not possess a real root.
Walter Roberson
Walter Roberson le 7 Jan 2016
In practice polyfit will not have leading 0's in the output except for the all-zero input.
John D'Errico
John D'Errico le 7 Jan 2016
Modifié(e) : John D'Errico le 7 Jan 2016
Polyfit will always return the coefficients of a 7th degree polynomial.
Will some of them be essentially zero? They will probably never be exactly zero, but MAY be floating point trash. And given this data, the probability is nil that they will ever be exactly zero.
Even for the extreme case that Roger poses,
p = polyfit(X,ones(1,8),7)
p =
3.3186e-12 -1.1719e-11 1.633e-11 -1.1339e-11 4.049e-12 -6.7884e-13 3.8415e-14 1
we see that the polynomial lacks zero coefficients, although they are all floating point trash except for the constant term, which was and should be 1.
roots(p)
ans =
39.822 + 18.933i
39.822 - 18.933i
10.215 + 42.543i
10.215 - 42.543i
-26.704 + 34.116i
-26.704 - 34.116i
-43.135 + 0i
Even there, roots does not recognize this as essentially a constant function, although arguably it should. Long ago some code I wrote to solve for the roots of a spline function catered to this case. There, we had to compute the roots of a cubic polynomial, that in some cases might degenerate into a quadratic, linear, or even constant function but with trash for one or more leading coefficient(s). So the equivalent of a direct roots computation could generate spurious roots.
Our solution was to reduce the order of the polynomial before the roots were computed, by looking at the leading coefficient(s). We even got tricky there as I recall, in how we threw away those high order terms, but that is a long story and would make this a quite long comment.
An interesting point arises though. Can you always infer that a leading coefficient for a polynomial is floating point trash?
To create a bad example, I'll do something here that I hope no sane person should ever do:
x = rand(1,8) + 200;
y = rand(1,8);
p = polyfit(x,y,7)
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as
described in HELP POLYFIT.
> In polyfit (line 75)
p =
-0.041779 40.54 -13951 1.3265e+06 3.7244e+08 -1.1795e+11 1.2527e+13 -4.8479e+14
See that the problem was very badly conditioned. The leading coefficient was essentially zero compared to the constant term. But if we raise numbers on the order of 200 to the 7th power, that leading order term becomes significant in the shape of the polynomial over the domain where the polynomial was actually fit.
Torsten
Torsten le 7 Jan 2016
Since we don't know the background of your question, we can not answer it.
Best wishes
Torsten.
mar vouz
mar vouz le 7 Jan 2016
thank you very much everyone!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Polynomials dans Centre d'aide et File Exchange

Commenté :

le 7 Jan 2016

Community Treasure Hunt

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

Start Hunting!

Translated by