%Representation of f=step function of problem 1 in hw 3 as sine series

clf, clear
L=4; x=linspace(0,L,1000);
f=ones(size(x)); n=length(x); f(n/2:n)=2*f(n/2:n);

ysin=zeros(size(x));
for n=1:1:100
   if mod(n,4)==0
     bn=0;
   elseif mod(n,2)==1
     bn=3;
   elseif mod(n,4)==2
     bn=-2;
   end
   bn=2*bn/(n*pi);
   ysin=ysin+ bn*sin(n*pi/L*x);
   plot(x,f,'b',x,ysin,'r','linewidth',2) 
   xlim([0,L]), ylim([-0.2,2.2])
   set(gca,'fontsize',20)
   xlabel('x'), ylabel('y')
   label=sprintf('N = %d',n);
   title('Sine series, partial sums')
   legend('f',label,'location','southeast')
   pause(.01)
%	pause
end