Computation of Fourier Series First define the signal. > > f:=x->x; f := x -> x > plot(f,-Pi..Pi,numpoints=200); #Plot the signal > (1/(2*Pi))*Int(f(x),x=-Pi..Pi); a0:=evalf(%); #the coefficient of a_0 Pi / | | x dx | / -Pi 1/2 ---------- Pi a0 := 0 > > a:=n-> value( (1/Pi)*Int(f(x)*cos(n*x),x=-Pi..Pi) ) ; # the value of a_n Pi / | | f(x) cos(n x) dx | / -Pi a := n -> value(----------------------) Pi > b:=n-> value((1/Pi)*Int(f(x)*sin(n*x),x=-Pi..Pi)) ; #the value of b_n Pi / | | f(x) sin(n x) dx | / -Pi b := n -> value(----------------------) Pi > a(n); b(n); 0 -sin(Pi n) + Pi n cos(Pi n) -2 --------------------------- 2 Pi n > S:=a0+Sum(a(n)*cos(n*x)+b(n)*sin(n*x),n=1..10); 10 ----- \ / (-sin(Pi n) + Pi n cos(Pi n)) sin(n x)\ S := ) |-2 --------------------------------------| / | 2 | ----- \ Pi n / n = 1 > S:=value(%); S := 2 sin(x) - sin(2 x) + 2/3 sin(3 x) - 1/2 sin(4 x) + 2/5 sin(5 x) - 1/3 sin(6 x) + 2/7 sin(7 x) - 1/4 sin(8 x) + 2/9 sin(9 x) - 1/5 sin(10 x) > plot({f(x), S}, x=-2*Pi..2*Pi); > f:=x->piecewise(x<-Pi/2,0,x piecewise(x < - 1/2 Pi, 0, x < 1/2 Pi, 1, 0) > plot(f,-Pi..Pi,numpoints=200); #Plot the signal > (1/(2*Pi))*Int(f(x),x=-Pi..Pi); a0:=value(%); #the coefficient of a_0 Pi / { 0 x < - 1/2 Pi | { | { 1 x < 1/2 Pi dx | { / { 0 otherwise -Pi 1/2 -------------------------------- Pi a0 := 1/2 > > a:=n-> value( (1/Pi)*Int(f(x)*cos(n*x),x=-Pi..Pi) ) ; # the value of a_n Pi / | | f(x) cos(n x) dx | / -Pi a := n -> value(----------------------) Pi > b:=n-> value((1/Pi)*Int(f(x)*sin(n*x),x=-Pi..Pi)) ; #the value of b_n Pi / | | f(x) sin(n x) dx | / -Pi b := n -> value(----------------------) Pi > a(n); b(n); sin(1/2 Pi n) 2 ------------- Pi n 0 > S:=a0+Sum(a(n)*cos(n*x)+b(n)*sin(n*x),n=1..5); / 5 \ |----- | | \ / sin(1/2 Pi n) cos(n x)\| S := 1/2 + | ) |2 ----------------------|| | / \ Pi n /| |----- | \n = 1 / > S:=value(%); cos(x) cos(3 x) cos(5 x) S := 1/2 + 2 ------ - 2/3 -------- + 2/5 -------- Pi Pi Pi > plot({f(x), S}, x=-2*Pi..2*Pi); > f:=x->piecewise(x<-Pi/2,0,x<0,x+Pi/2,x piecewise(x < - 1/2 Pi, 0, x < 0, x + 1/2 Pi, x < 1/2 Pi, 1/2 Pi - x, 0) > plot(f,-Pi..Pi,numpoints=200); #Plot the signal > (1/(2*Pi))*Int(f(x),x=-Pi..Pi); a0:=value(%); #the coefficient of a_0 Pi / { 0 x < - 1/2 Pi | { | { 1/2 Pi + x x < 0 | { dx | { 1/2 Pi - x x < 1/2 Pi | { / { 0 otherwise -Pi 1/2 ----------------------------------------- Pi a0 := 1/8 Pi > > a:=n-> value( (1/Pi)*Int(f(x)*cos(n*x),x=-Pi..Pi) ) ; # the value of a_n Pi / | | f(x) cos(n x) dx | / -Pi a := n -> value(----------------------) Pi > b:=n-> value((1/Pi)*Int(f(x)*sin(n*x),x=-Pi..Pi)) ; #the value of b_n Pi / | | f(x) sin(n x) dx | / -Pi b := n -> value(----------------------) Pi > a(n); b(n); cos(1/2 Pi n) - 1 -2 ----------------- 2 Pi n 0 > S:=a0+Sum(a(n)*cos(n*x)+b(n)*sin(n*x),n=1..3); / 3 \ |----- | | \ / (cos(1/2 Pi n) - 1) cos(n x)\| S := 1/8 Pi + | ) |-2 ----------------------------|| | / | 2 || |----- \ Pi n /| \n = 1 / > S:=value(%); cos(x) cos(2 x) cos(3 x) S := 1/8 Pi + 2 ------ + -------- + 2/9 -------- Pi Pi Pi > plot({f(x), S}, x=-Pi..Pi); >