Polyfit and polyval in Matlab

Hi!I have a problem with polyfit and polyval. I have two vectors (x and y). You can see it in the attached image (the red line). I want to create a polynom that allow to create the blue line (imagine that it doesn't exist) only with the values that I have.
I've tried with polyfit and polyfit but it doesn't work at all.
x2=x'; %I have changed the direction because if not, it doesn't do the polyfit.
coeffs1=polyfit(x2, msensiv_graf,1);
coeffs3=polyfit(x2, msensiv_graf,3);
coeffs5=polyfit(x2, msensiv_graf,5);
coeffs12=polyfit(x2, msensiv_graf,12);
new_y1 = polyval(coeffs1, x2);
new_y3 = polyval(coeffs3, x2);
new_y5 = polyval(coeffs5, x2);
new_y12 = polyval(coeffs12, x2);
plot(x2, msensiv_graf, 'r');
hold on
plot(x2, new_y1, 'r');
plot(x2, new_y3, 'b');
plot(x2, new_y5, 'g');
plot(x2, new_y12, 'y');
And the error is:
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.
Does somebody know what can I do? I also think to try interpolation to get the blue line.
Thanks a lot in advance.

 Accepted Answer

Image Analyst
Image Analyst on 13 Aug 2012
Edited: Image Analyst on 13 Aug 2012
Try using the S and mu outputs of polyfit(), like it (almost) says. I once had the same error and that helped me. Just get them and pass them into polyval() - it was as simple as that.

6 Comments

So, Emmanuelle, did that work for you?
This is my result. The red crosses are my points and the lines, is what I obtain with the following code:
coeffs1 = polyfit(x, nuevo, 1);
coeffs100 = polyfit(x, nuevo, 100);
new_y1 = polyval(coeffs1, x);
new_y3 = polyval(coeffs3, x);
new_y100 = polyval(coeffs100, x);
plot(x, nuevo, 'rx');
hold on
plot(x, new_y1, 'k');
plot(x, new_y100, 'g');
Image Analyst
Image Analyst on 22 Aug 2012
Edited: Image Analyst on 22 Aug 2012
Can you supply the numbers for x and y so we can run it? By the way, a polynomial of 100 is not much of a regression is it? Plus, I don't see that you even have 100 points so how can you do a 100th order "fit"? And where did coeffs3 come from? It seems some code is missing.
Sorry, the code is the following but I don't get any good result (I need a line that cross the different points, like an interpolation). Am I doing something wrong?
[coeffs1,S,mu] = polyfit(x, nuevo, 1); new_y1 = polyval(coeffs1, x); plot(x, nuevo, 'rx'); hold on plot(x, new_y1, 'k');
Did you read my prior comment about supplying input data we can work with?
Image Analyst, thanks for your reply. Yes,I've changed but it doesn't work at all. Now I'm trying directly with interpolation... (this is a capture of my result)
And this is what I want to get, like the example in Matlab's web:
My code:
  • plot(x2, nuevo); xt= 250:0.1:2500; yt= interp1(x2, nuevo, xt, 'spline'); plot(x2, nuevo, 'o', xt, yt);*

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!