Example 5.8
| > |
with(linalg):with(plots): |
| > |
ge1:=diff(u1(x,t),t)=diff(u1(x,t),x$2)-Phi^2*u1(x,t); |
 |
(1) |
| > |
ge2:=diff(u2(x,t),t)=diff(u2(x,t),x$2)-Phi^2*u2(x,t); |
 |
(2) |
 |
(3) |
| > |
bcalpha:=diff(u1(x,t),x)=diff(u2(x,t),x); |
 |
(4) |
 |
(5) |
 |
(6) |
 |
(7) |
The boundary condition u1(alpha,t) = u2(alpha,t) is satisfied by default.
Let N and M be the number of node points in region 1 and region 2 respetively.
 |
(8) |
 |
(9) |
 |
(10) |
 |
(11) |
| > |
dydxf:=1/2*(-u[2](t)-3*u[0](t)+4*u[1](t))/h1; |
), `-`(`*`(3, `*`(u[0](t)))), `*`(4, `*`(u[1](t)))))), `*`(h1)))](images/Example 5.8 Rev 1_12.gif) |
(12) |
| > |
dydxb:=1/2*(u[N+M](t)+3*u[N+M+2](t)-4*u[N+M+1](t))/h2; |
, `*`(3, `*`(u[12](t))), `-`(`*`(4, `*`(u[11](t))))))), `*`(h2)))](images/Example 5.8 Rev 1_13.gif) |
(13) |
| > |
dydxb2:=1/2*(u[N-1](t)+3*u[N+1](t)-4*u[N](t))/h1; |
, `*`(3, `*`(u[6](t))), `-`(`*`(4, `*`(u[5](t))))))), `*`(h1)))](images/Example 5.8 Rev 1_14.gif) |
(14) |
| > |
dydxf2:=1/2*(-u[N+3](t)-3*u[N+1](t)+4*u[N+2](t))/h2; |
), `-`(`*`(3, `*`(u[6](t)))), `*`(4, `*`(u[7](t)))))), `*`(h2)))](images/Example 5.8 Rev 1_15.gif) |
(15) |
The first and second derivatives are in the governing equation are converted to finite difference form as:
| > |
dydx:=piecewise(i<N+1,1/2/h1*(u[m+1](t)-u[m-1](t)),i>N+1,1/2/h2*(u[m+1](t)-u[m-1](t))); |
, `-`(u[`+`(m, `-`(1))](t))))), `*`(h1))), `<`(6, i), `+`(`/`(`*`(`/`(1, 2), `*`(`+`(u[`+`(m, 1)](t), `-`(u[`+`(m, `-`(1))](t))))), `...](images/Example 5.8 Rev 1_16.gif) |
(16) |
| > |
d2ydx2:=piecewise(i<N+1,1/h1^2*(u[m-1](t)-2*u[m](t)+u[m+1](t)),i>N+1,1/h2^2*(u[m-1](t)-2*u[m](t)+u[m+1](t))); |
, `-`(`*`(2, `*`(u[m](t)))), u[`+`(m, 1)](t))), `*`(`^`(h1, 2))), `<`(6, i), `/`(`*`(`+`(u[`+`(m, `-`(1))](t), `-`(`*`(2, `*`(u[m](t)))), u[`+`(m, ...](images/Example 5.8 Rev 1_17.gif) |
(17) |
| > |
bc1:=subs(diff(u1(x,t),x)=dydxf,u1(x,t)=u[0](t),x=0,bc1); |
, `-`(1))](images/Example 5.8 Rev 1_18.gif) |
(18) |
| > |
bc2:=subs(diff(u2(x,t),x)=dydxb,u2(x,t)=u[N+M+2](t),bc2); |
, `*`(3, `*`(u[12](t))), `-`(`*`(4, `*`(u[11](t))))))), `*`(h2)))](images/Example 5.8 Rev 1_19.gif) |
(19) |
| > |
bcalpha:=subs(diff(u1(x,t),x)=dydxb2,diff(u2(x,t),x)=dydxf2,u1(x,t)=u[N+1](t),u2(x,t)=u[N+1](t),bcalpha); |
, `*`(3, `*`(u[6](t))), `-`(`*`(4, `*`(u[5](t))))))), `*`(h1))) = `+`(`/`(`*`(`/`(1, 2), `*`(`+`(`-`(u[8](t)), `-`(`*`(3, `*`(u[6](t)))), `*`(4, `*`(u[7](t)))))),...](images/Example 5.8 Rev 1_20.gif) |
(20) |
, `-`(1))](images/Example 5.8 Rev 1_21.gif) |
(21) |
, `*`(3, `*`(u[6](t))), `-`(`*`(4, `*`(u[5](t))))))), `*`(h1))) = `+`(`/`(`*`(`/`(1, 2), `*`(`+`(`-`(u[8](t)), `-`(`*`(3, `*`(u[6](t)))), `*`(4, `*`(u[7](t)))))),...](images/Example 5.8 Rev 1_22.gif) |
(22) |
, `*`(3, `*`(u[12](t))), `-`(`*`(4, `*`(u[11](t))))))), `*`(h2)))](images/Example 5.8 Rev 1_23.gif) |
(23) |
When the governing equation is converted to finite difference form x has to be defined according to region 1 and region 2. The finite difference equations for region 1 are:
| > |
for i from 1 to N do eq[i]:=diff(u[i](t),t)= subs(diff(u1(x,t),x$2) = subs(m=i,d2ydx2),diff(u1(x,t),x) = subs(m=i,dydx),u1(x,t)=u[i](t),x=i*h1,rhs(ge1));od; |
eq[N+1] is given the boundary condition at the interface between region 1 and region 2. The finite difference equations for region 2 are:
| > |
for i from N+2 to N+M+1 do eq[i]:=diff(u[i](t),t)= subs(diff(u2(x,t),x$2) = subs(m=i,d2ydx2),diff(u2(x,t),x) = subs(m=i,dydx),u2(x,t)=u[i](t),x=alpha+(i-N-1)*h2,rhs(ge2));od; |
| > |
u[0](t):=(solve(eq[0],u[0](t))); |
 |
(26) |
| > |
u[N+1](t):=solve(eq[N+1],u[N+1](t)); |
)), `-`(`*`(4, `*`(h2, `*`(u[5](t))))), `*`(h1, `*`(u[8](t))), `-`(`*`(4, `*`(h1, `*`(u[7](t)))))))), `*`(`+`(h2, h1)))))](images/Example 5.8 Rev 1_35.gif) |
(27) |
| > |
u[N+M+2](t):=solve(eq[N+M+2],u[N+M+2](t)); |
))), `*`(`/`(4, 3), `*`(u[11](t))))](images/Example 5.8 Rev 1_36.gif) |
(28) |
| > |
for i from 1 to N do eq[i]:=eval(eq[i]):od:for i from N+2 to N+M+1 do eq[i]:=eval(eq[i]);od: |
| > |
eqs:=[seq(rhs(eq[j]),j=1..N),seq(rhs(eq[N+1+j]),j=1..M)]: |
| > |
Y:=[seq(u[i](t),i=1..N),seq(u[N+1+i](t),i=1..M)]; |
, u[2](t), u[3](t), u[4](t), u[5](t), u[7](t), u[8](t), u[9](t), u[10](t), u[11](t)]](images/Example 5.8 Rev 1_37.gif) |
(29) |
| > |
A:=genmatrix(eqs,Y,'b1'): |
| > |
b:=matrix(N+M,1):for i to N+M do b[i,1]:=-eval(b1[i]);od:evalm(b); |
![array( 1 .. 10, 1 .. 1, [( 5, 1 ) = 0, ( 9, 1 ) = 0, ( 7, 1 ) = 0, ( 1, 1 ) = `/`(1, `*`(`^`(h1, 2))), ( 6, 1 ) = 0, ( 8, 1 ) = 0, ( 4, 1 ) = 0, ( 3, 1 ) = 0, ( 10, 1 ) = 0, ( 2, 1 ) = 0 ] )](images/Example 5.8 Rev 1_38.gif) |
(30) |
| > |
h1:=eval(alpha/(N+1));h2:=eval((1-alpha)/(M+1)); |
 |
 |
(31) |
| > |
if N+M >4 then A:=map(evalf,A);end: |
| > |
Y0:=matrix(N+M,1):for i from 1 to N+M do Y0[i,1]:=0;od:evalm(Y0): |
| > |
s1:=evalm(Y0+inverse(A)&*b): |
| > |
Y:=evalm(mat&*s1-inverse(A)&*b): |
| > |
for i from 1 to N do u[i](t):=evalf((Y[i,1]));od: |
| > |
for i from N+2 to N+M+1 do u[i](t):=evalf((Y[i-1,1]));od: |
| > |
for i from 0 to N+M+2 do u[i](t):=eval(u[i](t));od; |
| > |
setcolors(["Red", "Blue", "LimeGreen", "Goldenrod", "maroon", "DarkTurquoise", "coral", "aquamarine", "magenta", "khaki", "sienna", "orange", "yellow", "gray"]); |
 |
(33) |
| > |
pp:=plot([seq(u[i](t),i=0..N+M+2)],t=0..0.1); |
![PLOT(CURVES([[0., 1.], [0.217971541666666648e-2, 1.], [0.407627414583333366e-2, 1.], [0.620915279166666702e-2, 1.], [0.835616954166666648e-2, 1.], [0.104929818958333332e-1, 1.], [0.124740804791666668e...](images/Example 5.8 Rev 1_91.gif) |
(34) |
| > |
pt:=textplot([[0.015,1.0,typeset(u[0],"(t)"),align=below],seq([0.02+i*0.005,evalf(subs(t=0.02+i*0.005,u[i](t))),typeset(u[i],"(t)"),align=above],i=1..N+M-2)]); |
![PLOT(TEXT([0.15e-1, 1.0], _TYPESET(u[0],](images/Example 5.8 Rev 1_92.gif) |
(35) |
| > |
display([pp,pt],title="Figure 5.27.",thickness=3,axes=boxed,labels=[t,"u"]); |
 |
(36) |
 |
(37) |
| > |
T1:=[seq(tf*i/MM,i=0..MM)]; |
| > |
PP:=matrix(N+M+3,MM+1); |
![array( 1 .. 13, 1 .. 31, [ ] )](images/Example 5.8 Rev 1_101.gif) |
(39) |
| > |
for i from 1 to N+M+3 do PP[i,1]:=0;od: |
| > |
for i from 1 to N+M+3 do for j from 2 to MM+1 do PP[i,j]:=evalf(subs(t=T1[j],u[i-1](t)));od;od: |
 |
(40) |
| > |
X:=piecewise(i<N+3,(i-1)*h1,i>N+2,alpha+(i-N-2)*h2); |
 |
(41) |
| > |
plot(X,i=1..N+M+3,thickness=4,title="Figure Exp.5.28.",axes=boxed,labels=['i',"X"]); |
| > |
plotdata:=[seq([ seq([eval(X),T1[j],PP[i,j]], i=1..N+M+3)], j=1..MM+1)]: |
| > |
surfdata(plotdata,axes=boxed,title="Figure Exp. 5.29.",labels=[x,t,u],orientation=[-75,60]); |
| > |
eq:=diff(U(x),x$2)-Phi^2*U(x); |
 |
(42) |
| > |
Ua:=rhs(dsolve({eq,U(0)=1,D(U)(1)=0},U(x))); |
 |
(43) |
| > |
Flux:=-evalf(subs(x=0.,diff(Ua,x))); |
 |
(44) |
| > |
flux:=limit(flux,t=infinity);Flux; |
 |
 |
(46) |
| > |
Err:=abs(flux-Flux)/Flux*100; |
 |
(47) |