Example 5.10 

> restart;
 

> with(linalg):with(plots):
 

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

diff(u(x, t), t) = `+`(`-`(diff(u(x, t), x))) (1)
 

> bc1:=u(x,t)-1;
 

`+`(u(x, t), `-`(1)) (2)
 

> bc2:=diff(u(x,t),t)=-diff(u(x,t),x);
 

diff(u(x, t), t) = `+`(`-`(diff(u(x, t), x))) (3)
 

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

u(x, 0) = 0 (4)
 

> N:=2;
 

2 (5)
 

> L:=1;
 

1 (6)
 

> dydxf:=1/2*(-u[2](t)-3*u[0](t)+4*u[1](t))/h:
 

> dydxb:=1/2*(u[N-1](t)+3*u[N+1](t)-4*u[N](t))/h:
 

> dydxb:=(u[N+1](t)-u[N](t))/h:
 

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

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

> bc1:=subs(diff(u(x,t),x)=dydxf,u(x,t)=u[0](t),x=0,bc1):
 

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

> eq[0]:=bc1;
 

`+`(u[0](t), `-`(1)) (7)
 

> eq[N+1]:=bc2;
 

diff(u[3](t), t) = `+`(`-`(`/`(`*`(`+`(u[3](t), `-`(u[2](t)))), `*`(h)))) (8)
 

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

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

1 (9)
 

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

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

 

 

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

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

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

> A:=genmatrix(eqs,Y,'b1'):
 

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

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

> A:=map(eval,A):
 

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

> evalm(A);
 

array( 1 .. 3, 1 .. 3, [( 2, 1 ) = 3, ( 1, 3 ) = 0, ( 1, 2 ) = 0, ( 2, 2 ) = -3, ( 3, 1 ) = 0, ( 3, 2 ) = 3, ( 2, 3 ) = 0, ( 3, 3 ) = -3, ( 1, 1 ) = -3 ] ) (11)
 

> J:=jordan(A,S);
 

array( 1 .. 3, 1 .. 3, [( 2, 1 ) = 0, ( 1, 3 ) = 0, ( 1, 2 ) = 1, ( 2, 2 ) = -3, ( 3, 1 ) = 0, ( 3, 2 ) = 0, ( 2, 3 ) = 1, ( 3, 3 ) = -3, ( 1, 1 ) = -3 ] ) (12)
 

> evalm(S);
 

array( 1 .. 3, 1 .. 3, [( 2, 1 ) = 0, ( 1, 3 ) = 1, ( 1, 2 ) = 0, ( 2, 2 ) = 3, ( 3, 1 ) = 9, ( 3, 2 ) = 0, ( 2, 3 ) = 0, ( 3, 3 ) = 0, ( 1, 1 ) = 0 ] ) (13)
 

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

array( 1 .. 3, 1 .. 3, [( 2, 1 ) = `+`(`*`(3, `*`(t, `*`(exp(`+`(`-`(`*`(3, `*`(t))))))))), ( 1, 3 ) = 0, ( 1, 2 ) = 0, ( 2, 2 ) = exp(`+`(`-`(`*`(3, `*`(t))))), ( 3, 1 ) = `+`(`*`(`/`(9, 2), `*`(`^`(... (14)
 

> Nrow:=rowdim(A):
 

> l:=evalf(eigenvalues(A));
 

-3., -3., -3. (15)
 

 

> for i to Nrow do lambda[i]:=l[i];od:
 

> Id:=Matrix(Nrow,Nrow,shape=identity):
 

> X:=matrix(Nrow,1,[seq(beta[i],i=1..Nrow)]):
 

> for k to Nrow do:
 

> G:=evalm((A-lambda[k]*Id)&*X):
 

> eqx[1]:=beta[1]=1:for i from 2 to Nrow do eqx[i]:=G[i-1,1]:od:
 

> for i to Nrow do beta[i]:=solve(eqx[i],beta[i]);od:
 

> XX[k]:=map(eval,evalm(X)):
 

> for i to Nrow do unassign('beta[i]'):od:od:
 

 

> P:=Matrix([seq(evalm(XX[i]),i=1..Nrow)]);
 

Matrix(%id = 153132480) (16)
 

> expD1:=Matrix(1..Nrow,1..Nrow,shape=diagonal):
 

> for i to Nrow do expD1[i,i]:=exp(lambda[i]*t):od:
 

> expD1:=map(convert,expD1,trig):
 

> mat:=evalm(P&*expD1&*inverse(P)):
 

Error, (in evalm) unnamed vector or array with undefined entries.
 

> mat:=map(expand,mat):
 

> mat:=map(simplify,mat):
 

> Y0:=matrix(N+1,1):for i from 1 to N+1 do Y0[i,1]:=evalf(subs(x=i*h,rhs(IC)));od:evalm(Y0):
 

> Y:=evalm(mat&*(Y0+inverse(A)&*b)-inverse(A)&*b):
 

> #b2:=subs(t=tau,evalm(b)):
 

> #mat2:=subs(t=t-tau,evalm(mat)):
 

> #mat3:=evalm(mat2&*b2):
 

> #mat4:=map(int,mat3,tau=0..t):
 

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

> for i from 1 to N+1 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:
 

> for i from 0 to N+1 do u[i](t):=subs(I=0,u[i](t));od:
 

> setcolors(["Red", "Blue", "LimeGreen", "Goldenrod", "maroon", "DarkTurquoise", "coral", "aquamarine", "magenta", "khaki",  "sienna", "orange", "yellow", "gray"]):
 

> pp:=plot([seq(subs(delta=1,u[i](t)),i=0..N+1)],t=0..1,thickness=4);
 

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

> pt:=textplot([seq([0.4,evalf(subs(delta=1,t=0.4,u[i](t))),typeset(u[i],"(t)"),align={below,right}],i=0..N+1)]);
 

PLOT(TEXT([.4, 1.], _TYPESET(u[0], (18)
 

> display([pp,pt],title="Figure Exp. 5.32.",axes=boxed,labels=[t,"u"]);
 

Plot_2d
 

> for i from 0 to N+1 do p[i]:=plot(subs(delta=0.1,u[i](t)),t=0..0.4,thickness=3);od:
 

> tf:=1;
 

1 (19)
 

> M:=30;
 

30 (20)
 

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

[0, `/`(1, 30), `/`(1, 15), `/`(1, 10), `/`(2, 15), `/`(1, 6), `/`(1, 5), `/`(7, 30), `/`(4, 15), `/`(3, 10), `/`(1, 3), `/`(11, 30), `/`(2, 5), `/`(13, 30), `/`(7, 15), `/`(1, 2), `/`(8, 15), `/`(17,...
[0, `/`(1, 30), `/`(1, 15), `/`(1, 10), `/`(2, 15), `/`(1, 6), `/`(1, 5), `/`(7, 30), `/`(4, 15), `/`(3, 10), `/`(1, 3), `/`(11, 30), `/`(2, 5), `/`(13, 30), `/`(7, 15), `/`(1, 2), `/`(8, 15), `/`(17,...
(21)
 

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

array( 1 .. 4, 1 .. 31, [ ] ) (22)
 

> for i from 1 to N+2 do PP[i,1]:=evalf(subs(x=(i-1)*h,rhs(IC)));od:
 

> for i from 1 to N+2 do for j from 2 to M+1 do PP[i,j]:=evalf(subs(t=T1[j],subs(delta=1,u[i-1](t))));od;od:
 

> plotdata := [seq([ seq([(i-1)*h,T1[j],PP[i,j]], i=1..N+2)], j=1..M+1)]:
 

> surfdata( plotdata, title="Figure Exp. 5.33.",axes=boxed, labels=[x,t,u],orientation=[45,45]);
 

Plot
 

>