% Fourier series example
% Lathi 6.1-1 (f)
% DRB 01-Apr-2010
% ============================================================
% USER PARAMETERS
t = -6:0.001:6;                 % time vector
T0 = 6;                         % fundamental period of x(t)
N = 5;                          % number of Fourier coefficients to generate
% ============================================================

x = zeros(1,length(t));
w0 = 2*pi/T0;
a0 = 0.5;                       % a_0
x = x+a0;                       % add in a_0 term
for n=1:N,
    an = (6/(pi^2*n^2))*(cos(n*pi/3)-cos(2*n*pi/3));    % a_n
    bn = 0;                                             % b_n
    x = x+an*cos(n*w0*t)+bn*sin(n*w0*t);                % add in nth harmonic
end

% make plot
plot(t,x);
grid on
xlabel('time');
ylabel('x(t)');
