% Periodic Fourier series, -2 < x < 2 % Step function at x = 1 format compact; NptsX=2000; % number of x pts Nf=5; % number of Fourier terms x=linspace(-5,5,NptsX); fs=8; figure(1) clf a0=1/4; a=zeros(1,Nf); b=zeros(1,Nf); f=a0*ones(1,NptsX); for n=1:Nf a(n)= -sin(n*pi/2)/(n*pi); % Fourier cosine coefficients b(n)=(cos(n*pi/2)-cos(n*pi))/(n*pi); % Fourier sine coefficients fn=a(n)*cos((n*pi*x)/2) + b(n)*sin((n*pi*x)/2); % Fourier function(n) f=f+fn; end set(gca,'FontSize',[fs]); plot(x,f,'b-'); hold on plot([-5,1],[0,0],'r-'); plot([1,5],[1,1],'r-'); xlabel('x','Fontsize',fs); ylabel('f(x)','Fontsize',fs); axis on; grid;