% sumOfSinusoids - sum of sinusoids with a "for" loop

F0 = 50;
Nmax = 50;
fs = F0*2*Nmax*4;

tt = 0:1/fs:3/F0;
xx = zeros( size(tt) );

figure(1)

for nn=1:Nmax
   A = 4/((2*nn-1)*pi);
   f = (2*nn-1)*F0;
   phi = -pi/2;
   
   xx = xx + A*cos(2*pi*f*tt + phi);
   
   % animation: see how the approximation gets better and better.
   plot(tt,xx)
   xlabel( 'Time (s)')
   grid;
   pause(0.3)
end

plot(tt,xx)
xlabel( 'Time (s)')
grid;