Recursive Functions É 155
The coefficients in the approximating polynomial of degree 5 are:
>> p = polyfit(x,y,5)
p =
-0.0076 0.0593 -0.0206 -0.4886 -0.0024 1.0001
There are six coefficients in p. To see how good the fit is, evaluate the polynomial at the
data points with the following:
>> f = polyval(p,x);
% A table showing the data, fit, and error is
>> table = [x y f y-f];
>> table(1:5,:)
ans =
% x y f y-f (error)
0 1.0000 1.0001 -0.0001
0.1000 0.9950 0.9949 0.0001
0.2000 0.9801 0.9800 0.0001
0.3000 0.9553 0.9553 0.0000
0.4000 0.9211 0.9211 -0.0000
From the above table, it can be seen that on this interval, the fit is good to three digits.
Let us evaluate the difference beyond the interval (0, pi) on which curve fitting was done.
>> x2 = pi * [11/10 ...