Example 3.7b  

> restart:
 

> with(linalg):with(plots):
 

> N:=4;
 

4 (1)
 

> L:=1;
 

1 (2)
 

> eq:=diff(y(x),x$2)-Pe*diff(y(x),x);
 

`+`(diff(diff(y(x), x), x), `-`(`*`(Pe, `*`(diff(y(x), x))))) (3)
 

> bc1:=y(x)=1;
 

y(x) = 1 (4)
 

> bc2:=y(x)=0;
 

y(x) = 0 (5)
 

The central difference expression for the second and first derivatives are 

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

`/`(`*`(`+`(y[`+`(m, 1)], `-`(`*`(2, `*`(y[m]))), y[`+`(m, `-`(1))])), `*`(`^`(h, 2))) (6)
 

> dydx:=(y[m]-y[m-1])/h;
 

`/`(`*`(`+`(y[m], `-`(y[`+`(m, `-`(1))]))), `*`(h)) (7)
 

The governing equation in finite difference form is: 

> Eq[m]:=subs(diff(y(x),x$2)=d2ydx2,diff(y(x),x)=dydx,y(x)=y[m],x=m*h,eq);
 

`+`(`/`(`*`(`+`(y[`+`(m, 1)], `-`(`*`(2, `*`(y[m]))), y[`+`(m, `-`(1))])), `*`(`^`(h, 2))), `-`(`/`(`*`(Pe, `*`(`+`(y[m], `-`(y[`+`(m, `-`(1))])))), `*`(h)))) (8)
 

A 'for loop' can be written for the interior node points as 

> for i to N do Eq[i]:=subs(m=i,Eq[m]);od;
 

 

 

 

`+`(`/`(`*`(`+`(y[2], `-`(`*`(2, `*`(y[1]))), y[0])), `*`(`^`(h, 2))), `-`(`/`(`*`(Pe, `*`(`+`(y[1], `-`(y[0])))), `*`(h))))
`+`(`/`(`*`(`+`(y[3], `-`(`*`(2, `*`(y[2]))), y[1])), `*`(`^`(h, 2))), `-`(`/`(`*`(Pe, `*`(`+`(y[2], `-`(y[1])))), `*`(h))))
`+`(`/`(`*`(`+`(y[4], `-`(`*`(2, `*`(y[3]))), y[2])), `*`(`^`(h, 2))), `-`(`/`(`*`(Pe, `*`(`+`(y[3], `-`(y[2])))), `*`(h))))
`+`(`/`(`*`(`+`(y[5], `-`(`*`(2, `*`(y[4]))), y[3])), `*`(`^`(h, 2))), `-`(`/`(`*`(Pe, `*`(`+`(y[4], `-`(y[3])))), `*`(h)))) (9)
 

> Eq[0]:=y[0]=1;
 

y[0] = 1 (10)
 

> Eq[N+1]:=y[N+1]=0;
 

y[5] = 0 (11)
 

> y[0]:=solve(Eq[0],y[0]);
 

1 (12)
 

> y[N+1]:=solve(Eq[N+1],y[N+1]);
 

0 (13)
 

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

`/`(1, 5) (14)
 

> for i to N do Eq[i]:=eval(Eq[i]);od;
 

 

 

 

`+`(`*`(25, `*`(y[2])), `-`(`*`(50, `*`(y[1]))), 25, `-`(`*`(5, `*`(Pe, `*`(`+`(y[1], `-`(1)))))))
`+`(`*`(25, `*`(y[3])), `-`(`*`(50, `*`(y[2]))), `*`(25, `*`(y[1])), `-`(`*`(5, `*`(Pe, `*`(`+`(y[2], `-`(y[1])))))))
`+`(`*`(25, `*`(y[4])), `-`(`*`(50, `*`(y[3]))), `*`(25, `*`(y[2])), `-`(`*`(5, `*`(Pe, `*`(`+`(y[3], `-`(y[2])))))))
`+`(`-`(`*`(50, `*`(y[4]))), `*`(25, `*`(y[3])), `-`(`*`(5, `*`(Pe, `*`(`+`(y[4], `-`(y[3]))))))) (15)
 

> eqs:=[seq(Eq[i],i=1..N)];
 

[`+`(`*`(25, `*`(y[2])), `-`(`*`(50, `*`(y[1]))), 25, `-`(`*`(5, `*`(Pe, `*`(`+`(y[1], `-`(1))))))), `+`(`*`(25, `*`(y[3])), `-`(`*`(50, `*`(y[2]))), `*`(25, `*`(y[1])), `-`(`*`(5, `*`(Pe, `*`(`+`(y[2...
[`+`(`*`(25, `*`(y[2])), `-`(`*`(50, `*`(y[1]))), 25, `-`(`*`(5, `*`(Pe, `*`(`+`(y[1], `-`(1))))))), `+`(`*`(25, `*`(y[3])), `-`(`*`(50, `*`(y[2]))), `*`(25, `*`(y[1])), `-`(`*`(5, `*`(Pe, `*`(`+`(y[2...
(16)
 

> vars:=[seq(y[i],i=1..N)];
 

[y[1], y[2], y[3], y[4]] (17)
 

> A:=genmatrix(eqs,vars,'B1');
 

Typesetting:-mrow(Typesetting:-mverbatim( (18)
 

> evalm(B1);
 

Typesetting:-mfenced(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mverbatim( (19)
 

Maple generates a row vector which can be converted to a column vector as: 

> B:=matrix(N,1):for i to N do B[i,1]:=B1[i]:od:evalm(B);
 

Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mtable(Typesetting:-mtr(Typesetting:-mtd(Typesetting:-mrow(Typesetting:-mverbatim( (20)
 

The solution is obtained as: 

> X:=evalm(inverse(A)&*B);
 

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

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

`/`(1, 5) (22)
 

> for i to N do y[i]:=X[i,1];od;
 

 

 

 

`+`(`-`(`/`(`*`(`/`(1, 5), `*`(`+`(500, `*`(150, `*`(Pe)), `*`(20, `*`(`^`(Pe, 2))), `*`(`^`(Pe, 3))), `*`(`+`(`-`(25), `-`(`*`(5, `*`(Pe))))))), `*`(`+`(3125, `*`(1250, `*`(Pe)), `*`(250, `*`(`^`(Pe,...
`+`(`-`(`/`(`*`(`/`(1, 5), `*`(`+`(`*`(`^`(Pe, 3)), `*`(20, `*`(`^`(Pe, 2))), `*`(150, `*`(Pe)), 375), `*`(`+`(`-`(25), `-`(`*`(5, `*`(Pe))))))), `*`(`+`(3125, `*`(1250, `*`(Pe)), `*`(250, `*`(`^`(Pe,...
`+`(`-`(`/`(`*`(`/`(1, 5), `*`(`+`(`*`(`^`(Pe, 3)), `*`(20, `*`(`^`(Pe, 2))), `*`(125, `*`(Pe)), 250), `*`(`+`(`-`(25), `-`(`*`(5, `*`(Pe))))))), `*`(`+`(3125, `*`(1250, `*`(Pe)), `*`(250, `*`(`^`(Pe,...
`+`(`-`(`/`(`*`(`/`(1, 5), `*`(`+`(`*`(`^`(Pe, 3)), `*`(15, `*`(`^`(Pe, 2))), `*`(75, `*`(Pe)), 125), `*`(`+`(`-`(25), `-`(`*`(5, `*`(Pe))))))), `*`(`+`(3125, `*`(1250, `*`(Pe)), `*`(250, `*`(`^`(Pe, ... (23)
 

> y[0]:=eval(y[0]);y[N+1]:=eval(y[N+1]);
 

 

1
0 (24)
 

> ya:=(exp(Pe)-exp(Pe*x))/(exp(Pe)-1);
 

`/`(`*`(`+`(exp(Pe), `-`(exp(`*`(Pe, `*`(x)))))), `*`(`+`(exp(Pe), `-`(1)))) (25)
 

> p1:=plot([seq([i*h,subs(Pe=1,y[i])],i=0..N+1)],thickness=4,color=blue,axes=boxed):
 

> p2:=plot(subs(Pe=1,ya),x=0..1,thickness=5,color=brown,axes=boxed,linestyle=2):
 

> display({p1,p2},title="Figure Exp. 3.1.11.",labels=[x,"y"]);
 

Plot_2d
 

> p1:=plot([seq([i*h,subs(Pe=50,y[i])],i=0..N+1)],thickness=4,color=green,axes=boxed):
 

> p2:=plot(subs(Pe=50,ya),x=0..1,thickness=5,color=brown,axes=boxed,linestyle=2):
 

> display({p1,p2},title="Figure Exp. 3.1.12.",labels=[x,"y"]);
 

Plot_2d
 

We observe that when the forward difference accurate to the order h2 is used, even when the  Peclet number is 1, there is a slight discrepancy between the finite difference solution and the analytical solution.  However, when the Peclet number is high (Pe = 50) the forward difference scheme does not five an unrealistic oscillation like the central difference scheme.  Note that for three digit accuracy with the analytical solution, N = 40 and 110 interior note points are required for central difference and forward difference approximations, respectively. 

>