And I would like to find the polynomial equation from this.
I've tried
p = polyfit(x,y,3);
with y2 = p(1)*x.^3 + p(2)*x.^2 + p(3)*x
but my y2 is not equal to the original y . What is wrong ?
Thanks you
And I would like to find the polynomial equation from this.
I've tried
p = polyfit(x,y,3);
with y2 = p(1)*x.^3 + p(2)*x.^2 + p(3)*x
but my y2 is not equal to the original y . What is wrong ?
Thanks you
Copyright © 2021 Jogjafile Inc.
As radarhead wrote in his comment, you forgot the coefficient of zero degree (
p(4)here).Assuming
xandyare vectors of same lengthn,polyfit(x,y,n-1)will return a vector containing the coefficients of the interpolating polynomial (of degreen-1) in descending order.Then, the value of the interpolating polynomial at a point
zwill be given by:Don't forget
p(4)! As Bas suggested, you can use thepolyvalfunction to easily compute the value of a polynomial at a a given point:To illustrate this, see the code below, which generates 4 data points, plots those points and the polynomial interpolating them: