Making the best fit for this data

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

I made this but there is small error in line (f(:,k)=polyval(polyfit(x,y,k),newx)) below what is the error
if true
% 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')
grid
pause(2)
newx=-7:0.5:9;
for k=1:9
f(:,k)=polyval(polyfit(x,y,k),newx)
figure(k);plot(newx,f(:,k),x,y,'o');
title(sprintf('Highest Degree Polynomial is k=%3.0f',k ))
string = ['Coef. are:' num2str(polyfit(x,y,k))];
disp(string)
grid
pause(1)
end
end
This section of your code runs for me without error:
x=[-7 -5 0 4 9];
y=[-343 -125 0 64 729];
newx=-7:0.5:9;
for k=1:numel(x)-1
f(:,k)=polyval(polyfit(x,y,k),newx);
figure(k);plot(newx,f(:,k),x,y,'o');
title(sprintf('Highest Degree Polynomial is k=%3.0f',k ))
string = ['Coef. are:' num2str(polyfit(x,y,k))];
disp(string)
grid
pause(1)
end
I redefined the upper limit for the for loop. Fitting a polynomial of the same degree (or higher) than the number of data you have is not valid.
it has error with the same previous line (
if true
% f(:,k)=polyval(polyfit(x,y,k),newx);
end
Please copy the entire error message (all the red text) from your Command Window, and paste it into your next Comment. Your code (the section that I quoted) ran without error (in R2017b). I could not reproduce the error, so that is also going to make it difficult for me to solve that problem.
Error in Ycode5 (line 5) f(:,k)=polyval(polyfit(x,y,k),newx);
This is the red message
Can you please post the output given to you (fitted plot)
Break it out into two lines:
p = polyfit(x,y,k);
f(:,k) = polyval(p, newx);
See what line throws the error. If the entire error message is what you quoted, I have no idea what the problem could be, since I cannot reproduce it.
Can you show me the output you got
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)')))
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

Categories

Asked:

on 3 Oct 2017

Commented:

on 3 Oct 2017

Community Treasure Hunt

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

Start Hunting!