%
% Example 3.1 and Figure 3.3
%
t=-1.5:0.01:1.5;
wo=4, E=1; 
% approximation with 10 harmonics
N=10;
xN=0;
for n=1:N
    an=(E/(n*pi))*(sin(n*pi/2)-sin(n*3*pi/2))
    xN=xN+an.*cos(n*wo*t);
end
% .* indicates the pointwise multiplication
subplot(221); plot(t,xN)
xlabel('time')
ylabel('approximation x10')
axis([-2 2 -0.7 0.7])
hold
% approximation with 20 harmonics
N=20;
xN=0;
for n=1:N
    an=(E/(n*pi))*(sin(n*pi/2)-sin(n*3*pi/2))
    xN=xN+an.*cos(n*wo*t);
end
subplot(222); plot(t,xN)
xlabel('time')
ylabel('approximation x20')
axis([-2 2 -0.7 0.7])
% approximation with 30 harmonics
N=30;
xN=0;
for n=1:N
    an=(E/(n*pi))*(sin(n*pi/2)-sin(n*3*pi/2))
    xN=xN+an.*cos(n*wo*t);
end
subplot(223); plot(t,xN)
xlabel('time')
ylabel('approximation x30')
axis([-2 2 -0.7 0.7])
% approximation with 50 harmonics
N=50;
xN=0;
for n=1:N
    an=(E/(n*pi))*(sin(n*pi/2)-sin(n*3*pi/2))
    xN=xN+an.*cos(n*wo*t);
end
subplot(224); plot(t,xN)
xlabel('time')
ylabel('approximation x50')
axis([-2 2 -0.7 0.7])
print -deps fig3_3.eps
