Graphing a polyfit function

1 visualización (últimos 30 días)
Karl Kropp-Sullivan
Karl Kropp-Sullivan el 30 de Abr. de 2021
Respondida: John D'Errico el 30 de Abr. de 2021
I have a set of data points. There are 23 points. I want to approximate a polynomial that will go through or near those points. First, I set x1=([-84.8058656, -83.4565941, ...) then y1=([41.69608411, 41.7311527, ...). I've been able to plot these points just fine.
Where I run into an issue is when I try to use polyfit to approximate a polynomial, p1, for the points and then I try to plot that polynomial. I've tried every degree polynomial from 1 to 100 and I either get an error or a polynomial that is way off what it should be.
I am not super familliar with MATLAB, could someone guide me through how they would go about doing that?
Thanks.

Respuestas (1)

John D'Errico
John D'Errico el 30 de Abr. de 2021
DON'T USE A HIGH ORDER POLYNOMIAL!
DON'T USE A HIGH ORDER POLYNOMIAL!
DON'T USE A HIGH ORDER POLYNOMIAL!
Do I need to say it again? I said it three times, so it must be true. High degree polynomials are a waste of computing power, and of your time. Yes, I know, you learned about Taylor series. What you have missed is many Taylor series are not convergent, and the only ones you get to play with as a student are the good ones.
What you did wrong I have not a clue. But it is trivial to fit a polynomial and then plot it.
x = rand(1,20);
y = sin(2*x) + randn(size(x))/10;
P3 = polyfit(x,y,3);
xpred = linspace(min(x),max(x),100);
ypred = polyval(P3,xpred);
plot(x,y,'bo',xpred,ypred,'r-')
Seems to work fine for me. Again, what you did wrong, we have no clue, because you don't tell us. If you really want help, then you need to show what you did.

Categorías

Más información sobre Polynomials en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by