function testfourier % PRE: - % POST: Plots the Fourier series of the two functions as % specified on the exercise sheet. % Implementation of the given functions: % One instance of f: f1 = @(t) abs(t).*(+(and(-0.5 <= t,t < 0.5))); % Periodized version: f = @(t) f1(t)+f1(t-1)+f1(t+1)+f1(t-2)+f1(t+2); % One instance of g: g1 = @(t) -(and(-0.50,t<0.5)); % Periodized version: g = @(t) g1(t)+g1(t-1)+g1(t+1)+g1(t-2)+g1(t+2); % For plotting: xx = linspace(-2,2,1e4); yy = f(xx); zz = g(xx); % Calculating the Fourier series: ff = 0.25*ones(size(xx)); gg = zeros(size(xx)); for k=1:19 ff = ff + ((-1)^k-1)./pi^2/k^2*cos(2*pi*k*xx); gg = gg + 2*(1-(-1)^k)/pi/k*sin(2*pi*k*xx); if (k==1) f1 = ff; g1 = gg; elseif (k==3) f3 = ff; g3 = gg; elseif (k==5) f5 = ff; g5 = gg; end end % Plotting plot(xx,yy,'k--',xx,f1,xx,f3,xx,f5,xx,ff); title('Fourier Series of f(t)'); xlabel('t'); ylabel('S_nf(t)'); legend('f','n=1','n=3','n=5','n=19'); figure; plot(xx,zz,'k--',xx,g1,xx,g3,xx,g5,xx,gg); title('Fourier Series of g(t)'); xlabel('t'); ylabel('S_ng(t)'); legend('f','n=1','n=3','n=5','n=19');