> # FOURIER SINE SERIES OF A SQUARE WAVE
>
> # f(x) = 1 on (0,Pi); f(x) = -1 on (-Pi,0); f has period 2*Pi.
>
> # First we need to calculate the Fourier coefficients.
> bn := (2/Pi)*Int(sin(n*x), x=0..Pi);
> value(%);
> # This is 0 unless n is odd.
>
> # Note that, unlike the coefficients of the triangle wave, these decrease like 1/n, not 1/n^2.
>
> bodd := k -> (4/Pi)*(1/(2*k+1));
> partialsum := K -> sum(bodd(k)*sin((2*k+1)*x), k=0..K);
> plot(partialsum(0), x=-8..8);
> plot(partialsum(1), x=-8..8);
> plot(partialsum(2), x=-8..8);
> plot(partialsum(4), x=-8..8);
> plot(partialsum(8), x=-8..8);
> plot(partialsum(16), x=-8..8);
> # Let's look up close.
> plot(partialsum(16), x=-1..4);
> plot(partialsum(32), x=-1..4);
> plot(partialsum(64), x=-1..4);
>