% Periodic Fourier series, -2 < x < 2 
% Step function at x = 1

NptsX=2000;          % number of x pts
Nf=200;              % number of Fourier terms
x=linspace(-5,5,NptsX);

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',16);
plot(x,f,'b-','LineWidth',1.5);
hold on
plot([-5,1],[0,0],'r-','LineWidth',1.5);
plot([1,5],[1,1],'r-','LineWidth',1.5);
xlabel('$x$','FontSize',16,'FontName',fontlabs, ...
    'interpreter','latex');  
ylabel('$y$','FontSize',16,'FontName',fontlabs, ...
    'interpreter','latex'); 
axis on; grid;

print -depsc eg200_gr.eps
