In: Advanced Math
f(x) = (x^2 )0 < x < 1, (2−x), 1 < x < 2
A) Solve this integral, writing An as an expression in terms of
n. Write down the
values of A1,A2,A3,A4,A5 correct to 8 significant
figures.
b) Use MATLAB to find the coefficients of the first five
harmonics and compare the
results with those from part (e). Your solution should include a
copy of the m-file
fnc.m which you use to obtain the coefficients
c) Using MATLAB, plot the function and its approximating five-term Fourier series.
%Matlab code for Fourier Series
clear all
close all
%function for which Fourier series have to find
%All time values
X=linspace(0,2,101);
for i=1:length(X)
if X(i)>=0 && X(i)<1
zz(i)=(X(i))^2;
else
zz(i)=(2-X(i))^2;
end
end
figure(1)
%Plotting the function
plot(X,zz,'r','Linewidth',3)
xlabel('x')
ylabel('f(x)')
title('Fourier series for given equation')
a1=X(1); b1=X(end);
l=(b1-a1)/2;
%Fourier series of the function for finding a and b
coefficients
for j=1:5
ss1=zz.*cos(j*pi*X/l);
%all a values of the Fourier series
aa(j)=(1/l)*trapz(X,ss1);
ss2=zz.*sin(j*pi*X/l);
%all b values of the Fourier series
bb(j)=(1/l)*trapz(X,ss2);
end
%a0 value of Fourier series
aa0=(1/l)*trapz(X,zz);
s=aa0/2;
%all an and bn terms
fprintf('Printing few terms for Fourier series for 1st
function\n')
for i=1:5
fprintf('\tThe value of a%d=%f and b%d=%f.
\n\n',i,aa(i),i,bb(i))
end
X=linspace(-4,4,1001);
%Fourier series of the function
for i=1:5
s=s+bb(i)*sin(i*pi*X/l)+aa(i)*cos(i*pi*X/l);
end
figure(1)
hold on
plot(X,s,'b')
box on
legend('Actual Data','5 terms','Location','best')
%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%