Example 5.1 rev 1.mw

Example 5.1 Heat Conduction in a Rectangular Slab 

 

> restart;
 

> with(linalg):with(plots):
 

Enter the governing equation here: 

> ge:=diff(u(x,t),t)=diff(u(x,t),x$2);
 

diff(u(x, t), t) = diff(diff(u(x, t), x), x) (1)
 

Enter the boundary condition at x = 0: 

> bc1:=u(x,t);
 

u(x, t) (2)
 

Enter the boundary condition at x = L: 

> bc2:=u(x,t);
 

u(x, t) (3)
 

Enter the initial condition: 

> IC:=u(x,0)=1;
 

u(x, 0) = 1 (4)
 

Enter the number of interior node points: 

> N:=4;
 

4 (5)
 

Enter the length of the domain: 

> L:=1;
 

1 (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;
 

`+`(`/`(`*`(`/`(1, 2), `*`(`+`(`-`(u[2](t)), `-`(`*`(3, `*`(u[0](t)))), `*`(4, `*`(u[1](t)))))), `*`(h))) (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;
 

`+`(`/`(`*`(`/`(1, 2), `*`(`+`(u[3](t), `*`(3, `*`(u[5](t))), `-`(`*`(4, `*`(u[4](t))))))), `*`(h))) (8)
 

Convert the boundary conditions to the finite difference form: 

> dydx:=1/2/h*(u[m+1](t)-u[m-1](t));
 

`+`(`/`(`*`(`/`(1, 2), `*`(`+`(u[`+`(m, 1)](t), `-`(u[`+`(m, `-`(1))](t))))), `*`(h))) (9)
 

> d2ydx2:=1/h^2*(u[m-1](t)-2*u[m](t)+u[m+1](t));
 

`/`(`*`(`+`(u[`+`(m, `-`(1))](t), `-`(`*`(2, `*`(u[m](t)))), u[`+`(m, 1)](t))), `*`(`^`(h, 2))) (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);
 

u[0](t) (11)
 

> bc2:=subs(diff(u(x,t),x)=dydxb,u(x,t)=u[N+1](t),x=1,bc2);
 

u[5](t) (12)
 

> eq[0]:=bc1;
 

u[0](t) (13)
 

> eq[N+1]:=bc2;
 

u[5](t) (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;
 

 

 

 

diff(u[1](t), t) = `/`(`*`(`+`(u[0](t), `-`(`*`(2, `*`(u[1](t)))), u[2](t))), `*`(`^`(h, 2)))
diff(u[2](t), t) = `/`(`*`(`+`(u[1](t), `-`(`*`(2, `*`(u[2](t)))), u[3](t))), `*`(`^`(h, 2)))
diff(u[3](t), t) = `/`(`*`(`+`(u[2](t), `-`(`*`(2, `*`(u[3](t)))), u[4](t))), `*`(`^`(h, 2)))
diff(u[4](t), t) = `/`(`*`(`+`(u[3](t), `-`(`*`(2, `*`(u[4](t)))), u[5](t))), `*`(`^`(h, 2))) (15)
 

The boundary values u[0](t) and u[N+1](t) are eliminated: 

> u[0](t):=(solve(eq[0],u[0](t)));
 

0 (16)
 

> u[N+1](t):=solve(eq[N+1],u[N+1](t));
 

0 (17)
 

The governing equations are simplified as: 

> for i from 1 to N do eq[i]:=eval(eq[i]);od;
 

 

 

 

diff(u[1](t), t) = `/`(`*`(`+`(`-`(`*`(2, `*`(u[1](t)))), u[2](t))), `*`(`^`(h, 2)))
diff(u[2](t), t) = `/`(`*`(`+`(u[1](t), `-`(`*`(2, `*`(u[2](t)))), u[3](t))), `*`(`^`(h, 2)))
diff(u[3](t), t) = `/`(`*`(`+`(u[2](t), `-`(`*`(2, `*`(u[3](t)))), u[4](t))), `*`(`^`(h, 2)))
diff(u[4](t), t) = `/`(`*`(`+`(u[3](t), `-`(`*`(2, `*`(u[4](t)))))), `*`(`^`(h, 2))) (18)
 

> eqs:=[seq(rhs(eq[j]),j=1..N)];
 

[`/`(`*`(`+`(`-`(`*`(2, `*`(u[1](t)))), 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))... (19)
 

> Y:=[seq(u[i](t),i=1..N)];
 

[u[1](t), u[2](t), u[3](t), u[4](t)] (20)
 

> A:=genmatrix(eqs,Y);
 

Typesetting:-mrow(Typesetting:-mverbatim( (21)
 

The node spacing, h, is evaluated here: 

> h:=eval(1/(N+1));
 

`/`(1, 5) (22)
 

The A matrix is simplified as: 

> A:=map(eval,A);
 

Typesetting:-mrow(Typesetting:-mverbatim( (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:
 

> evalm(A);
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mrow(Typesetting:-mo( (24)
 

The solution is obtained by finding the exponential matrix: 

> mat:=exponential(A,t):
 

> mat:=map(evalf,mat):
 

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);
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mn( (25)
 

> Y:=evalm(mat&*Y0):
 

> Y:=map(simplify,Y);
 

Typesetting:-mrow(Typesetting:-mverbatim( (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;
 

 

 

 

 

 

0
`+`(`*`(.7236067982, `*`(exp(`+`(`-`(`*`(9.549150288, `*`(t))))))), `*`(.2763932022, `*`(exp(`+`(`-`(`*`(65.45084971, `*`(t))))))))
`+`(`-`(`*`(.1708203934, `*`(exp(`+`(`-`(`*`(65.45084971, `*`(t)))))))), `*`(1.170820394, `*`(exp(`+`(`-`(`*`(9.549150288, `*`(t))))))))
`+`(`-`(`*`(.1708203934, `*`(exp(`+`(`-`(`*`(65.45084971, `*`(t)))))))), `*`(1.170820394, `*`(exp(`+`(`-`(`*`(9.549150288, `*`(t))))))))
`+`(`*`(.7236067982, `*`(exp(`+`(`-`(`*`(9.549150288, `*`(t))))))), `*`(.2763932022, `*`(exp(`+`(`-`(`*`(65.45084971, `*`(t))))))))
0 (27)
 

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... (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], (29)
 

> display({pp,pt},axes=boxed,thickness=3,title="Figure Exp. 5.1",labels=[t,"u"]);
 

Plot_2d
 

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): 

> tf:=0.1;
 

.1 (30)
 

Enter the number of time steps (excluding 0): 

> M:=30;
 

30 (31)
 

The time intervals are stored in T1: 

> T1:=[seq(tf*i/M,i=0..M)];
 

[0., 0.3333333333e-2, 0.6666666667e-2, 0.1000000000e-1, 0.1333333333e-1, 0.1666666667e-1, 0.2000000000e-1, 0.2333333333e-1, 0.2666666667e-1, 0.3000000000e-1, 0.3333333333e-1, 0.3666666667e-1, 0.400000...
[0., 0.3333333333e-2, 0.6666666667e-2, 0.1000000000e-1, 0.1333333333e-1, 0.1666666667e-1, 0.2000000000e-1, 0.2333333333e-1, 0.2666666667e-1, 0.3000000000e-1, 0.3333333333e-1, 0.3666666667e-1, 0.400000...
[0., 0.3333333333e-2, 0.6666666667e-2, 0.1000000000e-1, 0.1333333333e-1, 0.1666666667e-1, 0.2000000000e-1, 0.2333333333e-1, 0.2666666667e-1, 0.3000000000e-1, 0.3333333333e-1, 0.3666666667e-1, 0.400000...
[0., 0.3333333333e-2, 0.6666666667e-2, 0.1000000000e-1, 0.1333333333e-1, 0.1666666667e-1, 0.2000000000e-1, 0.2333333333e-1, 0.2666666667e-1, 0.3000000000e-1, 0.3333333333e-1, 0.3666666667e-1, 0.400000...
[0., 0.3333333333e-2, 0.6666666667e-2, 0.1000000000e-1, 0.1333333333e-1, 0.1666666667e-1, 0.2000000000e-1, 0.2333333333e-1, 0.2666666667e-1, 0.3000000000e-1, 0.3333333333e-1, 0.3666666667e-1, 0.400000...
(32)
 

> PP:=matrix(N+2,M+1);
 

array( 1 .. 6, 1 .. 31, [ ] ) (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]);
 

Plot
 

>