Making the best fit for this data

1 view (last 30 days)
Ali Alnemer
Ali Alnemer on 3 Oct 2017
Commented: Star Strider on 3 Oct 2017
Hi all,
I have a problem coding the best fit for these data
x=[-7 -5 0 4 9] y=[-343 -125 0 64 729]
I made this but it is not efficient
% x=[-7 -5 0 4 9]
y=[-343 -125 0 64 729]
coef=polyfit(x,y,1)
thebest=coef(1)*x+coef(2)
figure(1)
plot(x,thebest,x,y,'o')
xlabel('xaxis')
ylabel('yaxis')
title('X VS Y')
coef=polyfit(x,y,2)
thebest=coef(3)*x.^2+coef(2)*x+coef(1)
figure(2)
plot(x,thebest,x,y,'o')
xlabel('xaxis')
ylabel('yaxis')
title('X VS Y')
coef=polyfit(x,y,3)
thebest=coef(4)*x.^3+coef(3)*x.^2+coef(2)*x+coef(1)
figure(3)
plot(x,thebest,x,y,'o')
xlabel('xaxis')
ylabel('yaxis')
title('X VS Y')
end
etc
Could you please help me to make the best fit?
thank you

Answers (2)

Star Strider
Star Strider on 3 Oct 2017
Edited: Star Strider on 3 Oct 2017
First, use the polyval (link) function to evaluate the fit you got with the polyfit function.
Second, you can centre and scale the fit to your data (if necessary) by asking polyfit for two extra outputs, and then passing them to polyval:
[p,S,mu] = polyfit(x,y,n);
[y,delta] = polyval(p,x,S,mu);
See the documentation for a full description of the function options.
EDIT You can use The File Exchange polyparci (link) function with polyfit to estimate the parameter confidence limits. These are important because any parameter with confidence limits that include zero (that is, have opposite signs), are not needed in the polynomial fit. A model with all parameters having significant (same-sign) confidence intervals (with polyfit polynomial models, different ‘models’ are defined by different polynomial degrees), is the likely the ‘best’ model in this context.
This information will help you refine your model.
  10 Comments
Ali Alnemer
Ali Alnemer on 3 Oct 2017
Missing part of the error. Here is the full message
((((Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in Untitled74 (line 17) f(:,k)=polyval(polyfit(x,y,k),newx)')))
Star Strider
Star Strider on 3 Oct 2017
I put (1x4) cell array ‘p’ and (33x4) double matrix ‘f’ in the attached ‘Ali Alnemer p f.mat’ file.

Sign in to comment.


Kian Azami
Kian Azami on 3 Oct 2017
Edited: Kian Azami on 3 Oct 2017
If you use the application of Curve Fitting Tool in the app section, you can easily find the best fitting model for your data. For example I have tried and by the polynomial of the 3rd degree you can fit your data. The fitted model are attached to this answer. And the model as below:
Linear model Poly3:
f(x) = p1*x^3 + p2*x^2 + p3*x + p4
Coefficients (with 95% confidence bounds):
p1 = 1 (1, 1)
p2 = 1.063e-15 (-3.781e-14, 3.993e-14)
p3 = 1.657e-15 (-4.174e-13, 4.207e-13)
p4 = -1.688e-14 (-1.307e-12, 1.273e-12)
Goodness of fit:
SSE: 1.644e-26
R-square: 1
Adjusted R-square: 1
RMSE: 1.282e-13
Linear model Poly2:
f(x) = p1*x^2 + p2*x + p3
Coefficients (with 95% confidence bounds):
p1 = 3.537 (-7.267, 14.34)
p2 = 51.23 (-1.024, 103.5)
p3 = -66.21 (-530.6, 398.1)
Goodness of fit:
SSE: 4.413e+04
R-square: 0.9319
Adjusted R-square: 0.8637
RMSE: 148.5

Community Treasure Hunt

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

Start Hunting!