Fourier series, cosine series, sine series

sm212m6.mws,wdj,2-4-98

>

Fourier series

Given a function f(x) on the interval -L < x < L, the program below will compute a_n if n>0 and will compute a_0/2 if n=0.

> fourier_cos_coeff:=proc(f,L,n::integer)
#f is a function of x, 0<x<2L
#n is the index of a_n
#L is half the period
local x,a;
if n>0 then a:=int(f(x)*cos(n*Pi*x/L),x=(-L)..L)/L; fi;
if n=0 then a:=int(f(x),x=(-L)..L)/(2*L); fi;
RETURN(a);
end;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

>

> f:=x->piecewise(x<0,1,x>0,x);
fourier_cos_coeff(f,5,0);
fourier_cos_coeff(f,5,1);
f(x) is an odd function so it's Fourier series cosine coefficients are all zero. The two commands above return a_0/2 and a_1 where L=5.

[Maple Math]

[Maple Math]

[Maple Math]

>

Given a function f(x) on the interval -L < x < L, the program below will compute b_n if n>0.

> fourier_sin_coeff:=proc(f,L,n::integer)
#f is a function of x, 0<x<2L
#n is the index of b_n
#L is half the period
local x,b;
b:=int(f(x)*sin(n*Pi*x/L),x=(-L)..L)/L;
RETURN(b);
end;

[Maple Math]

>

> f:=x->piecewise(x<0,1,x>0,x);
fourier_sin_coeff(f,5,1);
fourier_sin_coeff(f,5,2);
Again, f(x) is odd so it should have some non-zero Fourier series sine coefficients. The above commands return b_1, b_2.

[Maple Math]

[Maple Math]

[Maple Math]

> S2:=fourier_cos_coeff(f,5,0)+add(fourier_cos_coeff(f,5,n)*cos(n*Pi*x/5)+fourier_sin_coeff(f,5,n)*sin(n*Pi*x/5),n=1..2);
S3:=fourier_cos_coeff(f,5,0)+add(fourier_cos_coeff(f,5,n)*cos(n*Pi*x/5)+fourier_sin_coeff(f,5,n)*sin(n*Pi*x/5),n=1..3);
S4:=fourier_cos_coeff(f,5,0)+add(fourier_cos_coeff(f,5,n)*cos(n*Pi*x/5)+fourier_sin_coeff(f,5,n)*sin(n*Pi*x/5),n=1..4);
Here Sn is the n-th partial Fourier series of f(x). As noted above these will only involve sine terms. As n increases these partial Fourier series will approximate f(x) better and better.

[Maple Math]

[Maple Math]

[Maple Math]

> plot([f(x),S2,S3,S4],x=-5..5);
Note that S4 (in blue) approximates f(x) better than S2 (in green).

[Maple Plot]

Cosine series

Given a function f(x) on the interval 0 < x < L, the program below will compute a_n if n>0 and will compute a_0/2 if n=0, where a_n is the n-th cosine series coefficient. Recall that the n-th cosine series coefficient is the same as the n-th Fourier series cosine coefficient only if f(x) is even .
These coefficients are really the Fourier series cosine coefficients of the
even extension of f(x), f_even(x).

> cosine_series_coeff:=proc(f,L,n::integer)
#f is a function of x, 0<x<L
#n is the index of a_n
#L is half the period
local x,a;
if n>0 then a:=2*int(f(x)*cos(n*Pi*x/L),x=0..L)/L; fi;
if n=0 then a:=int(f(x)*cos(n*Pi*x/L),x=0..L)/L; fi;
RETURN(a);
end;

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

>

> f:=x->x;
cosine_series_coeff(f,5,0);
cosine_series_coeff(f,5,1);
This time, f(x) is regarded as a function on 0 < x < 5. The first few cosine coefficients are computed.

[Maple Math]

[Maple Math]

[Maple Math]

> S2:=add(cosine_series_coeff(f,5,n)*cos(n*Pi*x/5),n=0..2);
S3:=add(cosine_series_coeff(f,5,n)*cos(n*Pi*x/5),n=0..3);
S4:=add(cosine_series_coeff(f,5,n)*cos(n*Pi*x/5),n=0..4);
plot([abs(f(x)),S2,S3,S4],x=0..5,y=0..5);
These partial cosine series Sn approximate f_even(x) better and better as n increases.

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Plot]

Maple also allows for picewise given functions. Suppose you want to enter a function f(x) which is defined to be 1 if x < Pi/2 and equal to 2 if x > Pi/2. You type:

> f:=x->piecewise(x<Pi/2,1,x>Pi/2,2);

[Maple Math]

We can compute the first few terms of the cosine series of f(x):

> S2:=add(cosine_series_coeff(f,Pi,n)*cos(n*x),n=0..2);
S3:=add(cosine_series_coeff(f,Pi,n)*cos(n*x),n=0..3);
S9:=add(cosine_series_coeff(f,Pi,n)*cos(n*x),n=0..9):
plot([f(x),S2,S3,S9],x=0..Pi);
Note that S9 (which was suppressed since it has so many terms) approximates f(x) better that S2.

[Maple Math]

[Maple Math]

[Maple Plot]

Sine series

Given a function f(x) on the interval 0 < x < L, the program below will compute b_n, where b_n is the n-th sine series coefficient. Recall that the n-th sine series coefficient is the same as the n-th Fourier series sine coefficient only if f(x) is odd . These coefficients are really the Fourier series sine coefficients of the odd extension of f(x), f_odd(x).

> sine_series_coeff:=proc(f,L,n::integer)
#f is a function of x, 0<x<L
#n is the index of b_n
#L is half the period
local x,b;
b:=2*int(f(x)*sin(n*Pi*x/L),x=0..L)/L;
RETURN(b);
end;

[Maple Math]

We use the same function as above, retyped below, and approximate it uses sine series instead of cosine series.

> f:=x->piecewise(x<Pi/2,1,x>Pi/2,2);

[Maple Math]

> sine_series_coeff(f,Pi,1);
sine_series_coeff(f,Pi,2);

[Maple Math]

[Maple Math]

> S2:=add(sine_series_coeff(f,Pi,n)*sin(n*Pi*x/Pi),n=1..2);
S3:=add(sine_series_coeff(f,Pi,n)*sin(n*Pi*x/Pi),n=1..3);
S4:=add(sine_series_coeff(f,Pi,n)*sin(n*Pi*x/Pi),n=1..4);
S9:=add(sine_series_coeff(f,Pi,n)*sin(n*Pi*x/Pi),n=1..9):
plot([f(x),S2,S3,S4,S9],x=0..Pi);
Note that S9 (which was suppressed since it has so many terms) approximates f(x) better that S2.

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Plot]

>