% visualizes the Fourier series approximation % for a special christmas tree-like function clear all; close all; % maximum depth of Fourier coefficients Nmax = 100; xn = 201; % this is the exact function xex = [0 0 1 1 2 2 3 3 4]; fex = [0 0.5 0.5 2 1 1.5 0.5 1 0]; c = zeros(Nmax,1); a = zeros(Nmax,1); b = zeros(Nmax,1); fapp = zeros(xn,Nmax+1); xapp = linspace(0,4,xn)'; fapp(:,1) = 7/8*ones(xn,1); for n=1:Nmax % compute the real and imaginary Fourier coefficents c(n) = -i*(3*(-i)^n+(-1)^n+i^n+1)/(4*pi*n) - (1-(-i)^n)/(pi^2*n^2); a(n) = real(2*c(n)); b(n) = -imag(2*c(n)); % compute the according approximation fapp(:,n+1) = fapp(:,n) + c(n)*exp(i*pi/2*n*xapp) + conj(c(n))*exp(-i*pi/2*n*xapp); %fapp(:,n+1) = fapp(:,n) + a(n)*cos(pi/2*n*xapp) + b(n)*sin(pi/2*n*xapp); end % which approximations should be displayed? nvec = [1 2 3 4 5 7 10 20 50 100]; leg = cell(1,3); leg{1} = 'exakt'; % create the plots for n=1:length(nvec) hold off; % plot the exact solution plot(xex,fex,'k-','LineWidth',2); hold on; set(gca,'FontSize',20); xlim([0 4]); ylim([0 2]); % plot the approximate solution plot(xapp,fapp(:,nvec(n)),'r-','LineWidth',3); ss = strcat('n=',int2str(nvec(n))); leg{2} = ss; if n>1 %plot the former solution also plot(xapp,fapp(:,nvec(n-1)),'r:','LineWidth',2); ss = strcat('n=',int2str(nvec(n-1))); leg{3} = ss; legend(leg); else legend(leg(1:2)); end pause; % uncomment these lines if the pictures should be printed %ss = strcat('print -depsc2 baum',int2str(n),'.eps'); %eval(ss); %ss = strcat('print -djpeg95 baum',int2str(n),'.jpg'); %eval(ss); end