Problem 3.1.4
Try different fits on periodic function y = exp( sin(t-1) )
Contents
Part (a), polynomial fit
%Use a polynomial fit to the data. clear all; format compact; t = linspace(0,2*pi,200)'; b = exp(sin(t-1)); p = 7; % degree of polynomial fit is p-1 A = zeros(length(t),p+1); for j=1:p+1 A(:,j) = t.^(j-1); end disp(' ') disp('Part (a) coefficients: ') c = (A'*A)\(A'*b) p = A*c; % evaluate polynomial at all t values in one line!
Part (a) coefficients:
c =
0.3263
1.6443
-3.8803
4.7017
-2.3206
0.5403
-0.0601
0.0026
Part (b), trig function fit
Trigonometic function fit uses periodic functions which have that property in common with the original data.
A = ones(length(t),5); A(:,2) = cos(t); A(:,3) = sin(t); A(:,4) = cos(2*t); A(:,5) = sin(2*t); disp(' ') disp('Part (b) coefficients: ') d = (A'*A)\(A'*b) trig = A*d; % again, compute the fits at all t values in one line!
Part (b) coefficients:
d =
1.2661
-0.9511
0.6107
0.1130
-0.2469
Part (c)
Plot the results for comparison.
plot(t,p,'-',t,trig,'--',t,b,'-.') axis([0 2*pi 0 3]) xlabel('t'); legend('6th degree poly','trig fit','actual','Location','NorthEast'); title('Problem 3.1.4 -- Fit of Periodic Function');
Discussion
The fit with the trigonometric functions is better near the ends because those functions for the interpolation are periodic, like the original function is. The polynomial does pretty well, but to achieve a specified level of error we expect that using a smaller number of trig functions should do the job.