Fitting a parabola using Matlab polyfit -


i looking fit parabola following data.

x = [-10:2:16]; y = [0.0334,0.0230,0.0145,0.0079,0.0033,0.0009,0.0006,0.0026,0.0067,0.0130,0.0213,0.0317,0.0440,0.0580];  [p,~,~] = polyfit(x,y,2);  x2 = linspace(-10,16,100); y2 = polyval(p,x2);  y3 = 0.0003.*x2.^2 -0.0006.*x2 + 0.0011; figure plot(x,y,'o',x2,y2,x2,y3) 

however, fit not match data @ all. after putting data excel , fitting using 2nd order polynomial there, nice fit. y = 0.0003x2 - 0.0006x + 0.0011 (excel truncating coefficients skews fit bit). happening polyfit data?

solved.

matlab checks how many outputs user requesting. since requested 3 outputs though wasn't using them, polyfit changes coefficients map different domain xhat.

if instead did:

p = polyfit(x,y,2); plot(x2,polyval(p,x2)); 

then achieve appropriate result. recover same answer using 3 outputs:

[p2,s,mu] = polyfit(x,y,2); xhat = (x2-mu(1))./mu(2) y4 = polyval(p2,xhat) plot(x2,y4) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -