Maple Three Coursework - Inner product, Fourier series

The following code is to define the fourier() procedure, It takes as it's input; a function to apply the fourier process to, and a number.

My code declares the variables k a and b as local to the procedure, then sets g to a0. While looping through k, the procedure adds ak and bk to g (there is no special case for b0).

fourier:= proc(f, n)
    local k, a, b;
    g:= (1/(2*Pi))*(int(f, x=-Pi..Pi));
    for k from 1 to n do
        a:= ((1/Pi)*int(f*cos(k*x), x=-Pi..Pi))*cos(k*x);
        b:= ((1/Pi)*int(f*sin(k*x), x=-Pi..Pi))*sin(k*x);
        g:= g + a + b;

I am really not sure why this works but the end result of this rather complicated equation, is that you start of with a function which displays a graph; the fourier series starts as either a sin or a cos wave then gradually fits itself to approximate your original function.

For more detailed explanations than mine, have a look at this site it even has an interactive applet to play with :)

fourier( 3*x, 2)

fourier( 3*x, 3)

fourier( 3*x, 5)

fourier( 3*x, 10)

fourier( 3*x, 15)

fourier( 3*x, 35)