Example 7.10.mw

Example 7.10 Heat Transfer in a Rectangle 

> Typesetting:-mrow(Typesetting:-mi(
 

> with(plots):
 

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

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

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

u(x, 0) = 1 (2)
 

> bc1:=u(x,t)=0;
 

u(x, t) = 0 (3)
 

> bc2:=u(x,t)=0;
 

u(x, t) = 0 (4)
 

> Eq:=subs(u(x,t)=X(x)*T(t),eq):
 

> Eq:=Eq/X(x)/T(t):
 

> Eq_T:=lhs(Eq)=-lambda^2:
 

> T(t):=rhs(dsolve({Eq_T,T(0)=T0},T(t)));
 

`*`(T0, `*`(exp(`+`(`-`(`*`(`^`(lambda, 2), `*`(t))))))) (5)
 

> Eq_X:=rhs(Eq)=-lambda^2:
 

> Eq_X:=expand(Eq_X*X(x));
 

diff(diff(X(x), x), x) = `+`(`-`(`*`(X(x), `*`(`^`(lambda, 2))))) (6)
 

The eigenvalue λ is obtained using the shooting technique.  The third condition at x = 0 is assumed for Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mrow(Typesetting:-mo( 

X2 = Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mrow(Typesetting:-mo( 

> eqlambda:=subs(X(x)=X(x,lambda),Eq_X):
 

> eqlambda:=diff(eqlambda,lambda):
 

> eqlambda:=subs(diff(X(x,lambda),lambda)=X2(x),eqlambda):
 

> eqlambda:=subs(X(x,lambda)=X(x),eqlambda);
 

diff(diff(X2(x), x), x) = `+`(`-`(`*`(X2(x), `*`(`^`(lambda, 2)))), `-`(`*`(2, `*`(X(x), `*`(lambda))))) (7)
 

The dependent variables are: 

> vars:=(X(x),X2(x));
 

X(x), X2(x) (8)
 

The initial conditions for X and X2 are: 

> ICs:=(X(0)=0,D(X)(0)=1,X2(0)=0,D(X2)(0)=0);
 

X(0) = 0, (D(X))(0) = 1, X2(0) = 0, (D(X2))(0) = 0 (9)
 

A tolerance 1e - 9 is set.  A scaling factor of 1/2 is used for obtaining eigenvalues. 

> tol:=1e-9;rho:=1/2;
 

 

0.1e-8
`/`(1, 2) (10)
 

Different initial guesses are used to predict the first ten eigenvalues: 

> lambdaguess:=[3,6,9,12,15,18,21,24,27,30];
 

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30] (11)
 

> MM:=nops(lambdaguess);
 

10 (12)
 

> Xexp:=0:
 

> for i from 1 to MM do
 

> lambda0:=lambdaguess[i];
 

> k:=1;err:=1;
 

> while err>tol do
 

> eqs:=subs(lambda=lambda0,Eq_X),subs(lambda=lambda0,eqlambda);
 

> Sol[i]:=dsolve({eqs,ICs},{vars},type=numeric,output=listprocedure);
 

> Xpred:=rhs(Sol[i](1)[2]);
 

> X2pred:=rhs(Sol[i](1)[4]);
 

> lambda1:=lambda0+rho*(Xexp-Xpred)/X2pred;
 

> err:=abs(lambda1-lambda0);
 

> lambda0:=lambda1;k:=k+1;
 

> end;
 

> l[i]:=lambda0;
 

> kk[i]:=k;
 

> Err[i]:=err;
 

> od:
 

The first ten eigenvalues are: 

> seq(l[i],i=1..MM);
 

3.141592666, 6.283185394, 9.424778160, 12.56637090, 15.70796362, 18.84955636, 21.99114908, 25.13274183, 28.27433459, 31.41592732
3.141592666, 6.283185394, 9.424778160, 12.56637090, 15.70796362, 18.84955636, 21.99114908, 25.13274183, 28.27433459, 31.41592732
(13)
 

The number of iterations required to get the eigenvalues are: 

> seq(kk[i],i=1..MM);
 

28, 29, 30, 28, 28, 28, 28, 27, 28, 29 (14)
 

The error associated with the eigenvalues is: 

> seq(Err[i],i=1..MM);
 

0.1e-8, 0.1e-8, 0.1e-8, 0., 0., 0., 0., 0., 0., 0. (15)
 

The first ten eigenfunctions are plotted as: 

> for i to MM do XX[i]:=subs(Sol[i],X(x)):od:
 

> for i to MM do p[i]:=plot(XX[i](x),x=0..1,thickness=3,title="Figure Exp. 7.23.",axes=boxed):od:
 

> display({seq(p[i],i=1..MM)},labels=[x,"X"]);
 

Plot_2d
 

The first eigenfunction does not cross the x axis.  The second eigenfunction crosses the x axis once.  Similarly, the nth eigenfunction crosses the x axis n - 1 times. 

> U:=eval(X(x)*T(t)):
 

The solution can be taken as: 

> Un:=A[n]*XX[n](x)*exp(-lambda[n]^2*t);
 

`*`(A[n], `*`(XX[n](x), `*`(exp(`+`(`-`(`*`(`^`(lambda[n], 2), `*`(t)))))))) (16)
 

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

> u(x,t):=Sum(Un,n=1..MM);
 

Sum(`*`(A[n], `*`(XX[n](x), `*`(exp(`+`(`-`(`*`(`^`(lambda[n], 2), `*`(t)))))))), n = 1 .. 10) (17)
 

The constant An is obtained as: 

> eq_An:=eval(subs(t=0,u(x,t)))=rhs(IC);
 

Sum(`*`(A[n], `*`(XX[n](x))), n = 1 .. 10) = 1 (18)
 

> I1:=int(X(x)^2,x=0..1):
 

> I2:=int(X(x),x=0..1):
 

> An:=I2/I1;
 

`/`(`*`(int(X(x), x = 0 .. 1)), `*`(int(`*`(`^`(X(x), 2)), x = 0 .. 1))) (19)
 

The coefficient An , n = 1.. .10 are numerically obtained as: 

> for j to MM do
 

> N:=200:I1:=0:I2:=0:
 

> for i from 1 to N-1 do I1:=I1+XX[j](i/N)^2*2/(2*N);I2:=I2+XX[j](i/N)*2/(2*N)od:
 

> I1:=I1+(XX[j](0)^2+XX[j](1)^2)/(2*N):
 

> I2:=I2+(XX[j](0)+XX[j](1))/(2*N):
 

> A[j]:=I2/I1;od:
 

> seq(A[j],j=1..MM);
 

3.999917395, -0.1708160921e-5, 3.999256880, -0.3978565951e-5, 3.997937659, -0.6108475765e-5, 3.995960133, -0.8302711514e-5, 3.993323724, -0.1021964161e-4
3.999917395, -0.1708160921e-5, 3.999256880, -0.3978565951e-5, 3.997937659, -0.6108475765e-5, 3.995960133, -0.8302711514e-5, 3.993323724, -0.1021964161e-4
(20)
 

> u(x,t):=eval(u(x,t)):
 

> ua:=evalf(u(x,t));
 

`+`(`*`(3.999917395, `*`(XX[1](x), `*`(exp(`+`(`-`(`*`(9.869604479, `*`(t)))))))), `-`(`*`(0.1708160921e-5, `*`(XX[2](x), `*`(exp(`+`(`-`(`*`(39.47841870, `*`(t))))))))), `*`(3.999256880, `*`(XX[3](x)...
`+`(`*`(3.999917395, `*`(XX[1](x), `*`(exp(`+`(`-`(`*`(9.869604479, `*`(t)))))))), `-`(`*`(0.1708160921e-5, `*`(XX[2](x), `*`(exp(`+`(`-`(`*`(39.47841870, `*`(t))))))))), `*`(3.999256880, `*`(XX[3](x)...
`+`(`*`(3.999917395, `*`(XX[1](x), `*`(exp(`+`(`-`(`*`(9.869604479, `*`(t)))))))), `-`(`*`(0.1708160921e-5, `*`(XX[2](x), `*`(exp(`+`(`-`(`*`(39.47841870, `*`(t))))))))), `*`(3.999256880, `*`(XX[3](x)...
`+`(`*`(3.999917395, `*`(XX[1](x), `*`(exp(`+`(`-`(`*`(9.869604479, `*`(t)))))))), `-`(`*`(0.1708160921e-5, `*`(XX[2](x), `*`(exp(`+`(`-`(`*`(39.47841870, `*`(t))))))))), `*`(3.999256880, `*`(XX[3](x)...
`+`(`*`(3.999917395, `*`(XX[1](x), `*`(exp(`+`(`-`(`*`(9.869604479, `*`(t)))))))), `-`(`*`(0.1708160921e-5, `*`(XX[2](x), `*`(exp(`+`(`-`(`*`(39.47841870, `*`(t))))))))), `*`(3.999256880, `*`(XX[3](x)...
(21)
 

> uu:=piecewise(t=0,rhs(IC),t>0,ua):
 

> plot3d(uu,x=1..0,t=0.3..0,axes=boxed,title="Figure Exp. 7.24.",labels=[x,t,"u"],orientation=[60,60]);
 

Plot
 

> plot([subs(t=0,uu),subs(t=0.01,uu),subs(t=0.05,uu),subs(t=0.1,uu),subs(t=0.2,uu)],x=0..1,axes=boxed,title="Figure Exp. 7.25.",thickness=5,labels=[x,"u"],legend=["t=0","t=0.01","t=0.05","t=0.1","t=0.2"]);
 

Plot_2d
 

>