Example 5.1 rev 1.mw
Example 5.1 Heat Conduction in a Rectangular Slab
| > |
with(linalg):with(plots): |
Enter the governing equation here:
| > |
ge:=diff(u(x,t),t)=diff(u(x,t),x$2); |
 |
(1) |
Enter the boundary condition at x = 0:
 |
(2) |
Enter the boundary condition at x = L:
 |
(3) |
Enter the initial condition:
 |
(4) |
Enter the number of interior node points:
 |
(5) |
Enter the length of the domain:
 |
(6) |
Enter the three point backward difference expression (accurate to the order h2) for the first derivative at x = L:
| > |
dydxf:=1/2*(-u[2](t)-3*u[0](t)+4*u[1](t))/h; |
), `-`(`*`(3, `*`(u[0](t)))), `*`(4, `*`(u[1](t)))))), `*`(h)))](images/Example 5.1 rev 1_7.gif) |
(7) |
Enter the three point central difference expression (accurate to the order h2) for the second derivative at x = L:
| > |
dydxb:=1/2*(u[N-1](t)+3*u[N+1](t)-4*u[N](t))/h; |
, `*`(3, `*`(u[5](t))), `-`(`*`(4, `*`(u[4](t))))))), `*`(h)))](images/Example 5.1 rev 1_8.gif) |
(8) |
Convert the boundary conditions to the finite difference form:
| > |
dydx:=1/2/h*(u[m+1](t)-u[m-1](t)); |
, `-`(u[`+`(m, `-`(1))](t))))), `*`(h)))](images/Example 5.1 rev 1_9.gif) |
(9) |
| > |
d2ydx2:=1/h^2*(u[m-1](t)-2*u[m](t)+u[m+1](t)); |
, `-`(`*`(2, `*`(u[m](t)))), u[`+`(m, 1)](t))), `*`(`^`(h, 2)))](images/Example 5.1 rev 1_10.gif) |
(10) |
The boundary conditions are stored in eq[0] and eq[N+1].
| > |
bc1:=subs(diff(u(x,t),x)=dydxf,u(x,t)=u[0](t),x=0,bc1); |
](images/Example 5.1 rev 1_11.gif) |
(11) |
| > |
bc2:=subs(diff(u(x,t),x)=dydxb,u(x,t)=u[N+1](t),x=1,bc2); |
](images/Example 5.1 rev 1_12.gif) |
(12) |
](images/Example 5.1 rev 1_13.gif) |
(13) |
](images/Example 5.1 rev 1_14.gif) |
(14) |
The governing equations are converted to the finite difference form:
| > |
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; |
The boundary values u[0](t) and u[N+1](t) are eliminated:
| > |
u[0](t):=(solve(eq[0],u[0](t))); |
 |
(16) |
| > |
u[N+1](t):=solve(eq[N+1],u[N+1](t)); |
 |
(17) |
The governing equations are simplified as:
| > |
for i from 1 to N do eq[i]:=eval(eq[i]);od; |
| > |
eqs:=[seq(rhs(eq[j]),j=1..N)]; |
))), u[2](t))), `*`(`^`(h, 2))), `/`(`*`(`+`(u[1](t), `-`(`*`(2, `*`(u[2](t)))), u[3](t))), `*`(`^`(h, 2))), `/`(`*`(`+`(u[2](t), `-`(`*`(2, `*`(u[3](t)))), u[4](t))...](images/Example 5.1 rev 1_25.gif) |
(19) |
| > |
Y:=[seq(u[i](t),i=1..N)]; |
, u[2](t), u[3](t), u[4](t)]](images/Example 5.1 rev 1_26.gif) |
(20) |
 |
(21) |
The node spacing, h, is evaluated here:
 |
(22) |
The A matrix is simplified as:
 |
(23) |
When more than four node points are used, the entries of the A matrix should be decimals. This problem can be handled using the 'map(A,evalf)' as given below.
| > |
if N > 4 then A:=map(evalf,A);end: |
 |
(24) |
The solution is obtained by finding the exponential matrix:
The initial condition is stored in the Y0 vector.
| > |
Y0:=matrix(N,1):for i from 1 to N do Y0[i,1]:=evalf(subs(x=i*h,rhs(IC)));od:evalm(Y0); |
 |
(25) |
 |
(26) |
Next the dependent variables can be stored in ui(t),i = 0..N+1.
| > |
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; |
Hence, an analytical solution in time is obtained for the dependent variables at all of the node points. One can plot the concentration profiles by:
| > |
pp:=plot([seq(u[i](t),i=0..N+1)],t=0..0.4); |
![PLOT(CURVES([[0., 0.], [0.871886166666666594e-2, 0.], [0.163050965833333346e-1, 0.], [0.248366111666666682e-1, 0.], [0.334246781666666660e-1, 0.], [0.419719275833333322e-1, 0.], [0.498963219166666666e...](images/Example 5.1 rev 1_39.gif) |
(28) |
| > |
pt:=textplot([[0.05,0.05,typeset(u[0],"(t), ",u[5],"(t)")],[0.1,0.2,typeset(u[1],"(t), ",u[4],"(t)")],[0.15,0.4,typeset(u[2],"(t), ",u[3],"(t)")]]); |
![PLOT(TEXT([0.5e-1, 0.5e-1], _TYPESET(u[0],](images/Example 5.1 rev 1_40.gif) |
(29) |
| > |
display({pp,pt},axes=boxed,thickness=3,title="Figure Exp. 5.1",labels=[t,"u"]); |
A three dimensional plot can be made by storing the solution in a matrix (PP). Enter the time up to which you want to plot your profiles (this time has to be changed depending on the problem):
 |
(30) |
Enter the number of time steps (excluding 0):
 |
(31) |
The time intervals are stored in T1:
| > |
T1:=[seq(tf*i/M,i=0..M)]; |
![array( 1 .. 6, 1 .. 31, [ ] )](images/Example 5.1 rev 1_49.gif) |
(33) |
The first column of PP is filled in by using the initial condition:
| > |
for i from 1 to N+2 do PP[i,1]:=evalf(subs(x=(i-1)*h,rhs(IC)));od: |
The remaining columns are filled in using the solution obtained:
| > |
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: |
Next, data points are stored in plotdata for obtaining a 3D plot using Maple's 'surfdata' command.
| > |
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 Exp. 5.2",labels=[x,t,u],orientation=[45,60]); |