Example 7.10.mw

Example 7.10 Heat Transfer in a Rectangle

> |

> | with(plots):

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

| (1)

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

| (2)

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

| (3)

> | bc2:=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)));

| (5)

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

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

| (6)

The eigenvalue λ is obtained using the shooting technique. The third condition at x = 0 is assumed for

X2 =

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

| (7)

The dependent variables are:

> | vars:=(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);

| (9)

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

> | tol:=1e-9;rho:=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];

| (11)

> | MM:=nops(lambdaguess);

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

| (13)

The number of iterations required to get the eigenvalues are:

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

| (14)

The error associated with the eigenvalues is:

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

| (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"]);

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

| (16)

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

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

| (17)

The constant An is obtained as:

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

| (18)

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

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

> | An:=I2/I1;

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

| (20)

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

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

| (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([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"]);

> |