Example 5.7 Semianalytical Method for PDEs wtih Known Initial Profiles
| > | restart; |
| > | with(linalg):with(plots): |
| > | ge:=diff(u(x,t),t)=Ds*diff(u(x,t),x$2); |
| (1) |
| > | bc1:=diff(u(x,t),x); |
| (2) |
| > | bc2:=u(x,t); |
| (3) |
| > | IC:=u(x,0)=cos(Pi*x/2/L); |
| (4) |
| > | L:=0.02; |
| (5) |
| > | Ds:=2e-5; |
| (6) |
| > | N:=10; |
| (7) |
| > | dydxf:=1/2*(-u[2](t)-3*u[0](t)+4*u[1](t))/h; |
| (8) |
| > | dydxb:=1/2*(u[N-1](t)+3*u[N+1](t)-4*u[N](t))/h; |
| (9) |
| > | dydx:=1/2/h*(u[m+1](t)-u[m-1](t)); |
| (10) |
| > | d2ydx2:=1/h^2*(u[m-1](t)-2*u[m](t)+u[m+1](t)); |
| (11) |
| > | bc1:=subs(diff(u(x,t),x)=dydxf,u(x,t)=u[0](t),x=0,bc1); |
| (12) |
| > | bc2:=subs(diff(u(x,t),x)=dydxb,u(x,t)=u[N+1](t),x=1,bc2); |
| (13) |
| > | eq[0]:=bc1; |
| (14) |
| > | eq[N+1]:=bc2; |
| (15) |
| > | for i from 1 to N do eq[i]:=diff(u[i](t),t)= subs(diff(u(x,t),x$2) = subs(m=i,d2ydx2),diff(u(x,t),x) = subs(m=i,dydx),u(x,t)=u[i](t),x=i*h,rhs(ge));od; |
| (16) |
| > | u[0](t):=(solve(eq[0],u[0](t))); |
| (17) |
| > | u[N+1](t):=solve(eq[N+1],u[N+1](t)); |
| (18) |
| > | for i from 1 to N do eq[i]:=eval(eq[i]);od; |
| (19) |
| > | eqs:=[seq(rhs(eq[j]),j=1..N)]; |
| (20) |
| > | Y:=[seq(u[i](t),i=1..N)]; |
| (21) |
| > | A:=genmatrix(eqs,Y); |
| (22) |
| > | h:=eval(L/(N+1)); |
| (23) |
| > | A:=map(eval,A); |
| (24) |
| > | if N > 4 then A:=map(evalf,A);end: |
| > | evalm(A); |
| (25) |
| > | mat:=exponential(A,t): |
| > | mat:=map(evalf,mat): |
| > | Y0:=matrix(N,1):for i from 1 to N do Y0[i,1]:=evalf(subs(x=i*h,rhs(IC)));od:evalm(Y0); |
![]() |
(26) |
| > | Y:=evalm(mat&*Y0): |
| > | Y:=map(simplify,Y); |
| (27) |
| > | for i from 1 to N do u[i](t):=evalf((Y[i,1]));od: |
| > | for i from 0 to N+1 do u[i](t):=eval(u[i](t));od; |
| (28) |
| > | tf:=20; |
| (29) |
Set the color list:
| > | setcolors(["Red", "Blue", "LimeGreen", "Goldenrod", "maroon", "DarkTurquoise", "coral", "aquamarine", "magenta", "khaki", "sienna", "orange", "yellow", "gray"]); |
| (30) |
| > | pp:=plot([seq(u[i](t),i=0..N+1)],t=0..tf,thickness=3); |
| (31) |
Plot the texts:
| > | arw:=arrow(<6.0,0.55>,<-5.0,-0.57>,width=[1/1000,relative=true],head_width=[1/50,relative=false],head_length=[1/5,relative=false]):
pt:=textplot([[11,0.6,typeset("Follow the arrow: ",u[0],"(t), ..., ",u[N+1],"(t).")]]): |
| > | display([pp,pt,arw],title="Figure Exp. 5.25.",axes=boxed,labels=[t,"u"]); |
![]() |
| > | tf:=20; |
| (32) |
| > | M:=30; |
| (33) |
| > | T1:=[seq(tf*i/M,i=0..M)]; |
| (34) |
| > | PP:=matrix(N+2,M+1); |
| (35) |
| > | for i from 1 to N+2 do PP[i,1]:=evalf(subs(x=(i-1)*h,rhs(IC)));od: |
| > | for i from 1 to N+2 do for j from 2 to M+1 do PP[i,j]:=evalf(subs(t=T1[j],u[i-1](t)));od;od: |
| > | plotdata:=[seq([ seq([(i-1)*h,T1[j],PP[i,j]], i=1..N+2)], j=1..M+1)]: |
| > | surfdata(plotdata,axes=boxed,title="Figure 5.26.",labels=[x,t,u],orientation=[45,60]); |
![]() |
| > |