MATLAB Answers

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

61 views (last 30 days)
Deva Narayanan
Deva Narayanan on 17 Dec 2018
Edited: 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
Bruno Luong
Bruno Luong on 17 Dec 2018
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.

Answers (2)

John D'Errico
John D'Errico on 17 Dec 2018
Edited: 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

Bruno Luong
Bruno Luong on 17 Dec 2018
Edited: 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),'-')

Community Treasure Hunt

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

Start Hunting!