%% Fourier-demo
% The Fourier series expansion for a square-wave is
% made up of a sum of odd harmonics, as shown here
% using MATLAB(R).

%% Add an odd ...
function fourier_demo
    t = 0:.1:pi*4;
    y = sin(t);
    updatePlot(t,y);
    
    %%
    % In each iteration of the for loop add an odd 
    % harmonic to y. As _k_ increases, the output 
    % approximates a square wave with increasing accuracy.

    for k = 3:2:9
        %%
        % Perform the following mathematical operation
        % at each iteration:
        % 
        % $$y=y+\frac{sin(k\,t)}{k}$$
        % 
        y = y + sin(k*t)/k;
        
        display(sprintf('When k = %.1f',k));
        display('Then the plot is:');
        updatePlot (t,y)
    end
    
end

% Even though the approximations are constantly  
% improving, they will never be exact because of the 
% Gibbs phenomenon, or ringing.

function updatePlot(t,x)
% Subfunction to update the plot
    cla
    plot(t,x) 
end
