% Construct a "square" shape out of Fourier components
x=0:0.01:1;
m=1:1:11; % Take first 11 components 
mm=2:2:12;
B(m)=4.*1./m./pi;   % calculated Fourier coefficients
B(mm)=0;
s1=sin(pi*x/1);    % sinusoidal shapes corresponding to components
s3=sin(3*pi*x/1);
s5=sin(5*pi*x/1);
s7=sin(7*pi*x/1);
s9=sin(9*pi*x/1);
s11=sin(11*pi*x/1);

r1=B(1)*s1;        % Fourier components with proper amplitudes
r3=B(3)*s3;
r5=B(5)*s5;
r7=B(7)*s7;
r9=B(9)*s9;
r11=B(11)*s11;

plot(x,1,x,r1,x,r3,x,r5,x,r7,x,r9,x,r11,x,r1+r3+r5+r7+r9+r11,'Linewidth',3)
hold on
plot(x,r1+r3+r5+r7+r9+r11,'r','Linewidth',6)
axis([0 1 -0.5 1.5])
hold off
%%
% Plot Fourier coefficients
plot(m,B(m),'d','MarkerSize',20)
axis([0 12 -0.5 1.5])
%%
% Simulate time oscillation
for j=1:200
    
    plot(x,r1*cos(1*j/200*2*pi),x,r3*cos(3*j/200*2*pi),x,r5*cos(5*j/200*2*pi),x,r7*cos(7*j/200*2*pi),x,r9*cos(9*j/200*2*pi),x,r11*cos(11*j/200*2*pi));
    axis([0 1 -1.5 1.5]);
    hold on
    plot(x,r1*cos(1*j/200*2*pi)+r3*cos(3*j/200*2*pi)+r5*cos(5*j/200*2*pi)+r7*cos(7*j/200*2*pi)+r9*cos(9*j/200*2*pi)+r11*cos(11*j/200*2*pi),'r','Linewidth',6);
    hold off
    
    G(j)=getframe;
end


