MATLAB Answers

0

how to get polynomial fit for the set of data points????

Asked by Deva Narayanan on 17 Dec 2018
Latest activity Edited by Bruno Luong
on 17 Dec 2018
i have set of data points and i want to get 2 nd degree piecewise polynomial equation for this point . i have used following matlab code for this purpose:
x = [x1 x2 ...];
y = [y1 y2 ...];
p = poly fit (x,y,2)
f = polyval (p,x);
plot (x,y,'o'x,f,'-')
but i unable to get exact polynomial equation . can any one explain me how to get correct value for this points ? and what is exact equation for this points

  9 Comments

but how can i findout the8th degree polynimial equations gives excat solution for this data points?
What I think you do not understand is that an 8th degree polynomial is just a REALLY bad idea. High order polynomial fits are exactly that.
But, in order to do such a fit, you absolutely MUST use the centering/scaling options in polyfit for this problem.
[P,S,MU] = polyfit(x,y,8)
P =
Columns 1 through 5
325.005626152819 -1343.44789019254 1117.33013188067 1481.95173702699 -1769.64864034309
Columns 6 through 9
-710.281823181912 1180.13771869376 -722.974333148199 10279.1591621409
S =
struct with fields:
R: [9×9 double]
df: 9
normr: 275.263953753075
MU =
-0.0205173532222222
0.000296418663882207
plot(x,y,'ro',xint,polyval(P,xint,[],MU),'b-')
So look carefully at that plot. The fit is actually quite "good", in one sense that the residual errors are tiny. However, the fit is in fact pure crapola. Look at the oscillations BETWEEN the data points. Look at the bumps, wiggles, etc. Sorry, but virtual crap. This is a fundamental characteristic of high order polynomial fits, andexactly what I would expect.
You need to decide what you want from this exercise. If your goal is merely to plot a smooth curve through the points, then a cubic interpolating spline is probably a great idea. For example, again the basic fitting tools gives this plot:
I tried it, even 8th order polynomial won't give exact solution. Not even close.
As John has noticed thet data seems to go to infinity of the left side of x.
No polynomial can have this behavior, sorry.
Is it hyperbolic or something else? Who know? (again only you who might know how the data are acquired/recorded/simulated).

Sign in to comment.

2 Answers

Answer by John D'Errico
on 17 Dec 2018
Edited by John D'Errico
on 17 Dec 2018

I see you have attached a .xlsx file.
xy = [-0.020830515 13256.65
-0.020827342 12829.94
-0.020817825 12434.39
-0.020801967 12069.29
-0.020779773 11733.98
-0.020751248 11427.76
-0.020716403 11149.94
-0.020675247 10899.84
-0.020627794 10676.78
-0.020574057 10480.08
-0.020514052 10309.03
-0.0204478 10162.97
-0.020375318 10041.2
-0.02029663 9943.04
-0.020211759 9867.804
-0.020120732 9814.806
-0.020023576 9783.362
-0.01992032 9772.784];
x = xy(:,1);
y = xy(:,2);
I can plot them. However, as I do, I see they do NOT look like a quadratic polynomial. That is simply not a quadratic I see plotted.
plot(x,y,'o')
I then used the basic fitting tools to plot a quadratic polynomial fit. As you see, I was correct. So asking for polyfit to produce THE quadratic polynomial exact fit is something that simply makes no sense. Sorry, but a basic quadratic will not fit those points exactly. It simply does not have the correct shape to do so. How you generated the points isan unknown to us. But it was not a basic quadratic polynomial, and trying to force it into that mold will always fail.
If I had to make a wild guess, I might wonder if it is possible that you have a hyperbolic relationship, which perhaps you have confused with a quadratic. After all, they are both conic sections.
ft = fittype('a + b./(1 + c*x)')
ft =
General model:
ft(a,b,c,x) = a + b./(1 + c*x)
mdl = fit(x,y,ft)
Warning: Start point not provided, choosing random start point.
> In curvefit.attention.Warning/throw (line 30)
In fit>iFit (line 299)
In fit (line 108)
mdl =
General model:
mdl(x) = a + b./(1 + c*x)
Coefficients (with 95% confidence bounds):
a = 9416 (9265, 9568)
b = 17.07 (13.39, 20.74)
c = 47.78 (47.73, 47.82)
plot(mdl)
hold on
plot(x,y,'o')
Note that this model is not in fact exact. However, it is far closer to a good fit than a quadratic polynomial.
untitled.jpg

  0 Comments

Sign in to comment.


Answer by Bruno Luong
on 17 Dec 2018
Edited by Bruno Luong
on 17 Dec 2018

You might fit parametric, so it meets yours question since it's polynomial.
xy=xlsread('E:\MATLAB\data points.xlsx',1)
x=xy(:,1);
y=xy(:,2);
t = (1:length(x))';
Px = polyfit(t,x,5);
Py = polyfit(t,y,5);
ti = linspace(min(t),max(t));
plot (x,y,'o',polyval(Px,ti),polyval(Py,ti),'-')

  0 Comments

Sign in to comment.