Making the best fit for this data
Afficher commentaires plus anciens
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
Réponses (2)
Star Strider
le 3 Oct 2017
Modifié(e) : Star Strider
le 3 Oct 2017
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 commentaires
Ali Alnemer
le 3 Oct 2017
Star Strider
le 3 Oct 2017
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.
Ali Alnemer
le 3 Oct 2017
Star Strider
le 3 Oct 2017
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.
Ali Alnemer
le 3 Oct 2017
Ali Alnemer
le 3 Oct 2017
Star Strider
le 3 Oct 2017
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.
Ali Alnemer
le 3 Oct 2017
Ali Alnemer
le 3 Oct 2017
Star Strider
le 3 Oct 2017
I put (1x4) cell array ‘p’ and (33x4) double matrix ‘f’ in the attached ‘Ali Alnemer p f.mat’ file.
Kian Azami
le 3 Oct 2017
Modifié(e) : Kian Azami
le 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
Catégories
En savoir plus sur Polynomials dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!