Example 6.1 Rev 1N2.mw

Example 6.1 N2 

> Typesetting:-mrow(Typesetting:-mi(
 

Enter the governing equation: 

> ge:=diff(u(x,y),y$2)=-epsilon^2*diff(u(x,y),x$2);
 

diff(diff(u(x, y), y), y) = `+`(`-`(`*`(`^`(epsilon, 2), `*`(diff(diff(u(x, y), x), x))))) (1)
 

Enter the boundary conditions: 

> bc1:=u(x,y)-0;
 

u(x, y) (2)
 

> bc2:=u(x,y)-0;
 

u(x, y) (3)
 

> bc3:=u(x,y)-0;
 

u(x, y) (4)
 

> bc4:=u(x,y)-1;
 

`+`(u(x, y), `-`(1)) (5)
 

> epsilon:=1;
 

1 (6)
 

Enter the finite difference approximations for the derivatives: 

> dydxf:=1/2/h*(-u[m+2](zeta)-3*u[m](zeta)+4*u[m+1](zeta));
 

`+`(`/`(`*`(`/`(1, 2), `*`(`+`(`-`(u[`+`(m, 2)](zeta)), `-`(`*`(3, `*`(u[m](zeta)))), `*`(4, `*`(u[`+`(m, 1)](zeta)))))), `*`(h))) (7)
 

> dydxb:=1/2/h*(u[m-2](zeta)+3*u[m](zeta)-4*u[m-1](zeta));
 

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

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

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

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

`/`(`*`(`+`(u[`+`(m, `-`(1))](zeta), `-`(`*`(2, `*`(u[m](zeta)))), u[`+`(m, 1)](zeta))), `*`(`^`(h, 2))) (10)
 

Convert the boundary conditions to finite difference form: 

> bc1:=subs(diff(u(x,y),x)=subs(m=0,dydxf),u(x,y)=u[0](zeta),x=0,bc1);
 

u[0](zeta) (11)
 

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

u[`+`(N, 1)](zeta) (12)
 

Enter the number of interior node points: 

> N:=2;
 

2 (13)
 

> eq[0]:=bc1;
 

u[0](zeta) (14)
 

> eq[N+1]:=bc2;
 

u[3](zeta) (15)
 

Convert the governing equation to finite difference from (equation 6.1.12): 

> for i from 1 to N do eq[N+1+i]:=diff(u[N+1+i](zeta),zeta)= subs(diff(u(x,y),x$2) = subs(m=i,d2ydx2),diff(u(x,y),x) = subs(m=i,dydx),u(x,y)=u[i](zeta),x=i*h,rhs(h^2/epsilon^2*ge));od;
 

 

diff(u[4](zeta), zeta) = `+`(`-`(u[0](zeta)), `*`(2, `*`(u[1](zeta))), `-`(u[2](zeta)))
diff(u[5](zeta), zeta) = `+`(`-`(u[1](zeta)), `*`(2, `*`(u[2](zeta))), `-`(u[3](zeta))) (16)
 

Enter the boundary values: 

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

0 (17)
 

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

0 (18)
 

> for i from 1 to N do eq[i]:=diff(u[i](zeta),zeta)=u[N+1+i](zeta);od;
 

 

diff(u[1](zeta), zeta) = u[4](zeta)
diff(u[2](zeta), zeta) = u[5](zeta) (19)
 

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

 

 

 

diff(u[1](zeta), zeta) = u[4](zeta)
diff(u[2](zeta), zeta) = u[5](zeta)
diff(u[4](zeta), zeta) = `+`(`*`(2, `*`(u[1](zeta))), `-`(u[2](zeta)))
diff(u[5](zeta), zeta) = `+`(`-`(u[1](zeta)), `*`(2, `*`(u[2](zeta)))) (20)
 

Generate the A matrix using the governing equations and the dependent variables 

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

[u[4](zeta), u[5](zeta), `+`(`*`(2, `*`(u[1](zeta))), `-`(u[2](zeta))), `+`(`-`(u[1](zeta)), `*`(2, `*`(u[2](zeta))))] (21)
 

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

[u[1](zeta), u[2](zeta), u[4](zeta), u[5](zeta)] (22)
 

> A:=genmatrix(eqns,Y,'b1');
 

array( 1 .. 4, 1 .. 4, [( 1, 3 ) = 1, ( 2, 2 ) = 0, ( 1, 4 ) = 0, ( 1, 2 ) = 0, ( 3, 1 ) = 2, ( 2, 3 ) = 0, ( 4, 3 ) = 0, ( 4, 4 ) = 0, ( 3, 2 ) = -1, ( 2, 1 ) = 0, ( 4, 1 ) = -1, ( 1, 1 ) = 0, ( 3, 3... (23)
 

Convert the entries of the A matrix as decimals if N is greater than two (as in chapter 5.1): 

> if N>2 then A:=map(evalf,A):end;
 

> `:=`(NRow, rowdim(A)); 1
 

4 (24)
 

> Typesetting:-mrow(Typesetting:-mn(
 

Error, illegal use of an object as a name
 

Typesetting:-mambiguous(Typesetting:-mrow(Typesetting:-mn(
 

> evalm(A);
 

array( 1 .. 4, 1 .. 4, [( 1, 3 ) = 1, ( 2, 2 ) = 0, ( 1, 4 ) = 0, ( 1, 2 ) = 0, ( 3, 1 ) = 2, ( 2, 3 ) = 0, ( 4, 3 ) = 0, ( 4, 4 ) = 0, ( 3, 2 ) = -1, ( 2, 1 ) = 0, ( 4, 1 ) = -1, ( 1, 1 ) = 0, ( 3, 3... (25)
 

> b:=matrix(2*N,1):for i from 1 to 2*N do b[i,1]:=-b1[i];od:evalm(b);
 

array( 1 .. 4, 1 .. 1, [( 3, 1 ) = 0, ( 2, 1 ) = 0, ( 4, 1 ) = 0, ( 1, 1 ) = 0 ] ) (26)
 

Note that for the example given the b vector is zero.  However, depending on the boundary conditions, bc3 and bc3, the b vector can be a function of ζ or a constant vector. 

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

`/`(1, 3) (27)
 

> J:=jordan(A,S);
 

array( 1 .. 4, 1 .. 4, [( 1, 3 ) = 0, ( 2, 2 ) = 1, ( 1, 4 ) = 0, ( 1, 2 ) = 0, ( 3, 1 ) = 0, ( 2, 3 ) = 0, ( 4, 3 ) = 0, ( 4, 4 ) = `+`(`-`(`*`(`^`(3, `/`(1, 2))))), ( 3, 2 ) = 0, ( 2, 1 ) = 0, ( 4, ... (28)
 

> mat:=evalm(S&*exponential(J,zeta)&*inverse(S)):
 

> mat1:=evalm(subs(zeta=zeta-zeta1,evalm(mat))):
 

> b2:=evalm(subs(zeta=zeta1,evalm(b))):
 

> mat2:=evalm(mat1&*b2):
 

> mat2:=map(expand,mat2):
 

> mat3:=map(int,mat2,zeta1=0..zeta):
 

The initial condition vector is defined here. 

> Y0:=matrix(2*N,1);
 

array( 1 .. 4, 1 .. 1, [ ] ) (29)
 

> for i to N do Y0[i,1]:=p[i];od:
 

> for i to N do Y0[N+i,1]:=c[i]:od:
 

> evalm(Y0);
 

array( 1 .. 4, 1 .. 1, [( 3, 1 ) = c[1], ( 2, 1 ) = p[2], ( 4, 1 ) = c[2], ( 1, 1 ) = p[1] ] ) (30)
 

The solution is found by adding the nonhomogeneous part to the homogeneous part. 

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

The solution at y = 0 and y = 1 is stored in sol0 and sol1 to calculate the unknown constants. 

> sol0:=map(eval,evalm(subs(zeta=0,evalm(Y)))):
 

> sol1:=map(eval,evalm(subs(zeta=epsilon/h,evalm(Y)))):
 

Now the boundary conditions bc3 and bc4 are applied. 

> for i to N do Eq[i]:=subs(diff(u(x,y),y)=epsilon/h*c[i],u(x,y)=p[i],x=i*h,bc3);od;
 

 

p[1]
p[2] (31)
 

> for i to N do Eq[N+i]:=evalf(subs(diff(u(x,y),y)=epsilon/h*sol1[N+i,1],u(x,y)=sol1[i,1],bc4));od;
 

 

`+`(`*`(50.17924626, `*`(p[1])), `-`(`*`(40.11158426, `*`(p[2]))), `*`(31.07205648, `*`(c[1])), `-`(`*`(21.05418156, `*`(c[2]))), `-`(1.))
`+`(`-`(`*`(40.11158426, `*`(p[1]))), `*`(50.17924626, `*`(p[2])), `-`(`*`(21.05418156, `*`(c[1]))), `*`(31.07205648, `*`(c[2])), `-`(1.)) (32)
 

The unknown constants are solved as: 

> csol:=solve({seq(Eq[i],i=1..2*N)},{seq(c[i],i=1..N),seq(p[i],i=1..N)});
 

{p[2] = 0., c[2] = 0.9982156974e-1, c[1] = 0.9982156974e-1, p[1] = 0.} (33)
 

> assign(csol);
 

> Y:=map(eval,Y):
 

> for i from 1 to N do u[i](zeta):=eval((Y[i,1]));od:
 

> for i from 0 to N+1 do u[i](zeta):=eval(u[i](zeta));od:
 

> for i from 0 to N+1 do u[i](y):=eval(subs(zeta=epsilon*y/h,u[i](zeta)));od;
 

 

 

 

0
`+`(`-`(`/`(`*`(0.9982156974e-1, `*`(exp(`+`(`-`(`*`(3, `*`(y))))))), `*`(`+`(1, `*`(`^`(3, `/`(1, 2)))), `*`(`+`(`*`(`^`(3, `/`(1, 2))), `-`(1)))))), `*`(0.4991078488e-1, `*`(exp(`+`(`*`(3, `*`(y))))...
`+`(`-`(`/`(`*`(0.9982156974e-1, `*`(exp(`+`(`-`(`*`(3, `*`(y))))))), `*`(`+`(1, `*`(`^`(3, `/`(1, 2)))), `*`(`+`(`*`(`^`(3, `/`(1, 2))), `-`(1)))))), `*`(0.4991078488e-1, `*`(exp(`+`(`*`(3, `*`(y))))...
0 (34)
 

Hence, the semianalytical solution is obtained for temperature distribution.  The plots obtained for N=10 node points is given below: 

> for i from 0 to N+1 do
 pl[i]:=line([0.3,0.98-abs(i-5.25)*0.14],[0.6,evalf(subs(y=0.6,u[i](y)))],thickness=1,linestyle=dot);
 pt[i]:=textplot([0.3,0.98-abs(i-5.25)*0.14,typeset(u[i],"(y)")],align=left):
end do:
 

> pp:=plot([seq(u[i](y),i=0..N+1)],y=0..1);
 

PLOT(CURVES([[0., 0.], [0.217971541666666658e-1, 0.], [0.407627414583333348e-1, 0.], [0.620915279166666667e-1, 0.], [0.835616954166666648e-1, 0.], [.104929818958333324, 0.], [.124740804791666660, 0.],... (35)
 

> display([pp,seq(pl[i],i=0..N+1),seq(pt[i],i=0..N+1)],axes=boxed,thickness=3,title="Figure Exp. 6.1.",labels=[y,"u"]);
 

Plot_2d
 

> M:=10;
 

10 (36)
 

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

[0., .1000000000, .2000000000, .3000000000, .4000000000, .5000000000, .6000000000, .7000000000, .8000000000, .9000000000, 1.]
[0., .1000000000, .2000000000, .3000000000, .4000000000, .5000000000, .6000000000, .7000000000, .8000000000, .9000000000, 1.]
(37)
 

> for j from 1 to M do P[j]:=plot([seq([h*i,evalf(subs(y=T1[j],evalf(u[i](y))))],i=0..N+1)],style=line,thickness=3,axes=boxed,view=[0..1,0..1.1]):od:
 

> P[M+1]:=plot([seq([h*i,evalf(subs(x=i*h,1))],i=0..N+1)],style=line,thickness=3,title="Figure Exp. 6.2.",axes=boxed):
 

> for j from 1 to M+1 do
 pt[j]:=textplot([0.5,evalf(subs(y=T1[j],u[5](y))),typeset(y,sprintf("=%4.2f",T1[j]))],align=above);
od:
 

> display({seq(P[i],i=1..M+1),seq(pt[j],j=1..M+1)},labels=[x,u]);
 

Error:TEXT location must be numeric; received: [.5, u[5](.1000000000)]
 

> Ny:=30;
 

30 (38)
 

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

array( 1 .. 4, 1 .. 30, [ ] ) (39)
 

> for i to Ny do PP[1,i]:=0;PP[N+2,i]:=0;od:
 

> for i to N+2 do PP[i,1]:=0;PP[i,Ny]:=1;od:
 

> for i from 2 to N+1 do for j from 2 to Ny-1 do PP[i,j]:=evalf(subs(y=(j-1)/(Ny-1),u[i-1](y)));od;od:
 

> plotdata := [seq([ seq([(i-1)/(N+1),(j-1)/(Ny-1),PP[i,j]], i=1..N+2)], j=1..Ny)]:
 

> surfdata(plotdata,axes=boxed,title="Figure Exp. 6.3.",labels=[x,y,u],orientation=[-120,60] );
 

Plot
 

>