MATLAB Curve Fitting using (A) Straight line Fit (B). Least Squares Fit
p = polyfit(x,y,n)
IT FINDS THE COEFFICIENTS OF THE POLYNOMIAL OF ORDER N
SUPPOSE
x=[4 16 36 64]
y=[2 4 6 8]
plot(y,x);
Now we have some polynomial equation line ,if we want to find out the coefficients of the equation
Then we have to write
Polyfit(x,y,degree of polynomial)
Now we take degree as 2
>> Coefficients=Polyfit(x,y,2)
>> Coefficients =
-0.0010 0.1652 1.4400
Now we can write
Y=-0.0010x2 + 0.1652x + 1.4400
POLYVAL()
Y=x2 − 4x + 7 evaluated at x=1,2,3,4
c=[1 -4 7]
Polyval(c,[1,2,3,4])
>> c=[1 -4 7]
c =
1 -4 7
>> Polyval(c,[1,2,3,4])
ans =
4 3 4 7>> P=[1 2 3 4 5]
P =
1 2 3 4 5
>> Q=[2 5 9 10 59]
Q =
2 5 9 10 59
>> plot(P,Q,'o')
>> coefficents = polyfit(P,Q,1) finds coefficients for a first degree line
coefficents =
11.9000 -18.7000
>> R = polyval(coefficents, P) use coefficients from above to estimate a new set of y values
R =
-6.8000 5.1000 17.0000 28.9000 40.8000
>> plot(P,Q,'*',P,R,':') plot the original data with * and the estimated line with LINE