> with(plots): # input the data functions; try for example: f=sinx g=0; f=hat g=0, # f=sinx g neq 0; f=0 g= small step # > f:=x->(Pi/4-abs(x-Pi/2))*(Heaviside(x-Pi/4)-Heaviside(x-3*Pi/4)); > g:=x->0; > f := x -> (1/4 Pi - | x - 1/2 Pi |) (Heaviside(x - 1/4 Pi) - Heaviside(x - 3/4 Pi)) g := 0 # input the number of terms and generate the Fourier coeffs. # > N:=10; > b:=array(1..N); > > B:=array(1..N); > for k to N do b[k]:=evalf(2/Pi*int(sin(k*x)*f(x),x=0..Pi)) od: > for k to N do B[k]:=evalf(2/Pi*int(sin(k*x)*g(x),x=0..Pi)) od: N := 10 b := array(1 .. 10, []) B := array(1 .. 10, []) # generate the partial sums of the solution and the data # > u:=(x,t)->sum('(evalf(b[n])*cos(n*t)+B[n]/n*sin(n*t))*sin(n*x)','n'=1. > .N); > sf:=x->sum('evalf(b[n])*sin(n*x)','n'=1..N); > sg:=x->sum('evalf(B[n])*sin(n*x)','n'=1..N); > G:=x->int(g(v),v=0..x); > sG:=x->sum('evalf(B[n])*int(sin(n*w), w=0..x)','n'=1..N); u := (x, t) -> N ----- \ / B[n] sin(n t)\ ) '|evalf(b[n]) cos(n t) + -------------| sin(n x)' / \ n / ----- 'n' = 1 N ----- \ sf := x -> ) 'evalf(b[n]) sin(n x)' / ----- 'n' = 1 N ----- \ sg := x -> ) 'evalf(B[n]) sin(n x)' / ----- 'n' = 1 x / | G := x -> | g(v) dv | / 0 N x ----- / \ | sG := x -> ) 'evalf(B[n]) | sin(n w) dw' / | ----- / 'n' = 1 0 # generate the plots of the data, their partial sums, and the solution # > plot3d(u(x,t),x=0..Pi,t=0..10,title=`u(x,t)`); > plot(sG(x),x=0..Pi,title=`Partial sums of G`,color=red); > plot(G(x),x=0..Pi,title=`antiderivative G`,color=red); > plot(sg(x),x=0..Pi,title=`Partial sum of f(x)`,color=orange); > plot(g(x),x=0..Pi,title=`g(x)`,color=orange); > plot(sf(x),x=0..Pi,title=`Partial sum of f(x)`,color=blue); > plot(f(x),x=0..Pi,title=`f(x)`,color=blue); # # animate the traveling waves given by the data # > > animate(sf(x-t),x=0..Pi,t=0..10,frames=50,color=blue,title=`f moving > to the right`); > animate(sf(x+t),x=0..Pi,t=0..10,frames=50,color=yellow,title=`f moving > to the left`); > animate(sG(x-t),x=0..Pi,t=0..10,frames=50,color=blue,title=`G moving > to the right`); > animate(sG(x+t),x=0..Pi,t=0..10,frames=50,color=yellow,title=`G moving > to the left`); # # # # animation of the solution and the solution and the data # > a1:=animate(u(x,t),x=0..Pi,t=0..10,frames=50,color=green,title=`soluti > on and data`): > a2:=animate(1/2*sf(x-t),x=0..Pi,t=0..10,frames=50,color=blue): > a3:=animate(1/2*sf(x+t),x=0..Pi,t=0..10,frames=50,color=yellow): > a4:=animate(1/2*sG(x-t),x=0..Pi,t=0..10,frames=50,color=blue): > a5:=animate(1/2*sG(x+t),x=0..Pi,t=0..10,frames=50,color=yellow): > display({a1,a2,a3,a4,a5}); > animate(u(x,t),x=0..Pi,t=0..10,frames=50,color=green,title=`solution`) > ; >