%% Problem 3.1.4 % Try different fits on periodic function % y = exp( sin(t-1) ) %% 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 (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 (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.