how to fix the warning message with polyfit?

x_data = 1:33;
f_data = [2 7 5 5 8 9 10 9 15 14 25 45 50 35 48 74 102 147 171 199 226 208 200 150 121 54 27 30 12 10 10 4 5];
plot(x_data, f_data, 'ro')
axis([0, 35, -50, 300])
hold on
[a, s, mu] = polyfit(x_data, f_data, length(x_data)-1);
x = linspace(0, 35, 200);
y = polyval(a, x, s, mu);
plot(x, y)
xlabel('x')
ylabel('y')
legend('Data points', 'Interpolating polynomial', 'Location', 'NW')
I tried to plot a graph with the code above, and it's showing an Warning: Polynomial is badly conditioned. Add points with distinct X values or reduce the degree of the polynomial.

댓글 수: 5

XIN JIN
XIN JIN 2015년 3월 10일
I've been asked to construct an interpolating polynomial with the given data, and explain whether it is a good representation of the data or not. I don't want to plot with spline or any other tools. So what could I do to improve the condition?
John D'Errico
John D'Errico 2015년 3월 10일
편집: John D'Errico 2015년 3월 10일
Well, you see the plot I created from the interpolating polynomial. Does it look like a good representation to you? To me, "garbage" is the best description of that curve.
As I said, even if you do as much as you can do in terms of "improving" the condition, it will still look much the same. That is the nature of high order interpolating polynomials, and probably the reason you were asked to do this.
This is one of the many reasons why people use splines for modeling. We are taught as students about Taylor series - polynomial approximations to functions, so we think they would be useful. And if a low order polynomial fits reasonably well, then a high order one must be better. It rarely is better.
Interpolating polynomials are nice for textbooks. Easy to build, easy to teach. They do teach you some useful things about approximation. But for real problems? A good thing to avoid, like you would avoid the plague. It is this last part that too few texts and teachers seem to pass along. So I hope what you are expected to take away from this exercise is how to find better ways to solve interpolation problems.
Well a spline is an interpolating polynomial - it's a cubic! It's just that you have a new cubic equation between every pair of points, so it's like a piecewise cubic interpolating polynomial. Won't that satisfy your definition of interpolating polynomial? It interpolates, and it's a polynomial, so there you go.
XIN JIN
XIN JIN 2015년 3월 10일
I just want to get rid of the warning message. Is there a way to do that?
Well you can turn a specific warning off, using the function warning. Nothing stops you from doing this. Sorry, but it is a rather bad idea in general. That warning was put there for a very good reason - to tell you when you are doing something silly.
Turning that warning off is the equivalent of closing your eyes, and pretending nothing happened.

댓글을 달려면 로그인하십시오.

답변 (4개)

John D'Errico
John D'Errico 2015년 3월 10일
편집: John D'Errico 2015년 3월 10일

0 개 추천

Why "fix" it? The message is telling you that you are doing something foolish. It is a good thing here. Even if you tried centering and scaling the independent variable, you will still find that the condition number of the resulting matrix will be too large to yield useful results.
And finally, polynomials are a terrible tool to fit curves like that. The oscillations you see are an expected behavior. Use a spline to interpolate instead.
(And learn how to format your code as posted so it is readable.)
Jos (10584)
Jos (10584) 2015년 3월 10일

0 개 추천

Are you sure you want to fit your data with a polynomial of degree that scales with the number of elements of X? Moreover, is a polynomial really the best fit option?
Image Analyst
Image Analyst 2015년 3월 10일

0 개 추천

You don't want to use a regression technique to do an interpolation. You can use a spline like John said. Here is the code with your data:
% Demo to show spline interpolation.
% Clean up / initialize
clc;
workspace; % Display workspace panel.
% Create the original knot points.
y = [2 7 5 5 8 9 10 9 15 14 25 45 50 35 48 74 102 147 171 199 226 208 200 150 121 54 27 30 12 10 10 4 5];
x = 1:length(y);
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2, 'MarkerSize', 15);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 200 points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(1, length(x), 200);
smoothedY = spline(x, y, newXSamplePoints);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob', 'MarkerSize', 5);
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
% Mathworks Demo code from their Help
% x = 0:10;
% y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
The original points are the big red squares, and the interpolated points are the small blue circles.
Mark Alexander
Mark Alexander 2015년 4월 2일
편집: Mark Alexander 2015년 4월 2일

0 개 추천

This question relates to a course assignment, which tries to teach you why using certain nodes for a non-piecewise polynomial interpolation is a bad idea. So yes, (s)he does want to do it the 'wrong' way; that is the point of the assignment.
Now that the deadline for the assignment has passed (hello coursemate!), and for posterity, you can disable the error using:
warning('off', 'MATLAB:polyfit:RepeatedPoints');

댓글 수: 3

Wow, we never would have guessed that. He never mentioned it (suppressing the warning rather than avoiding it) in the original question, nor did he tag it as Homework (which I've done now).
John D'Errico
John D'Errico 2015년 4월 3일
편집: John D'Errico 2015년 4월 3일
No, the point is, you don't want to disable warnings like that. This is the effective equivalent of closing your eyes, thus pretending that nothing silly is being done. A warning hurts nothing.
That it was for homework, for a course is not relevant. And teaching students to do something foolish (just turning off an important warning) is itself a flat out bad idea.
And yes, that probably WAS the point of the assignment, as was getting that warning message to teach them why they don't want to do what it was they were doing. The assignment was NOT designed how to teach them to do something foolish though.
Oh, I didn't intend to 'correct' the already provided answers, of course you could never have guessed this was for an assignment or the parameters thereof.
Nonetheless, I can't see the harm in 'catching' and handling entirely expected and understood warnings. We know the fit is bad, and why it is bad. It's standard practice (at least outside of Matlab) to do something like
try:
some operations
catch(someSpecificException):
do something for someSpecificException
when we are expecting that someSpecificException be thrown. We don't always want it naively spit out in the middle of our command window output. The technical difference between the purpose of warnings and exceptions aside, I don't think suppressing one specific warning that we expect and understand is particularly egregious, especially if we are providing our own commentary to the same effect.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

질문:

2015년 3월 10일

편집:

2015년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by