Example 6.5 Rev 1 digits 10.mw

Example 6.5 Potential Distribution in a Hull Cell

> | restart;with(plottools):with(linalg):with(plots):

> |

> | ge:=diff(u(x,y),y$2)=-diff(u(x,y),x$2);

| (1)

> | Digits:=10;

| (2)

> | bc1:=diff(u(x,y),x);

| (3)

> | bc2:=diff(u(x,y),x);

| (4)

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

| (5)

> | bc4:=u(x,y);

| (6)

> | eq_cathode:=y=1+0.5*x;

| (7)

> | epsilon:=1;

| (8)

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

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

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

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

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

| (9)

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

| (10)

> | N:=10;

| (11)

> | eq[0]:=bc1;

| (12)

> | eq[N+1]:=bc2;

| (13)

> | 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),diff(u(x,y),y)=epsilon/h*u[N+1+i](zeta),u(x,y)=u[i](zeta),x=i*h,rhs(h^2/epsilon^2*ge));od:

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

| (14)

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

| (15)

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

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

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

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

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

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

> | evalm(A):

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

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

| (16)

> | J:=jordan(A,S):

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

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

| (17)

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

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

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

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

| (18)

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

> | for i to N do Eq[N+i]:=evalf(subs(zeta=epsilon/h*(1+0.5*i*h),Eq[N+i]));od:

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

| (19)

> | assign(csol);

> | YY:=map(eval,Y):

> | for i from 1 to N do u[i](zeta):=eval((YY[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:

> | for i from 0 to N+1 do p[i]:=plot(u[i](y),y=0..(1+0.5*i*h),thickness=2);od:

> | for i from 0 to N+1 do pl[i]:=line([1.2,0.3+i*0.05],[0.9,evalf(subs(y=0.9,u[i](y)))],thickness=1,linestyle=solid); pt[i]:=textplot([1.2,0.3+i*0.05,typeset(u[i],"(y)")],align=right): end do:

> | display([seq(p[i],i=0..N+1,2),seq(pl[i],i=0..N+1,2),seq(pt[i],i=0..N+1,2)],axes=boxed,title="Fig. Exp. 6.13.",labels=[y,"u"]);

> | M:=10;

| (20)

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

| (21)

> | for j from 1 to M+1 do P[j]:=plot([seq([h*i,evalf(subs(y=T1[j]*(1+0.5*i*h),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,0))],i=0..N+1)],style=line,thickness=3,axes=boxed):

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

> | display({seq(P[i],i=1..M+1),seq(pt[i],i=1..M+1)},title="Fig. Exp. 6.14.",labels=[x,u]);

> | Ny:=30;

| (22)

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

| (23)

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

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

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

> | surfdata(plotdata,axes=boxed,title="Fig. Exp. 6.15.", labels=[x,y,u],orientation=[45,45] );

> | surfdata(plotdata,axes=boxed,title="Fig. Exp. 6.16.",labels=[x,y,u],orientation=[120,0] ,style=patchnogrid);

> | for i from 1 to N do curr[i]:=evalf(subs(y=1+0.5*i*h,diff(u[i](y),y)));od:

> | curr[0]:=4/3*curr[1]-1/3*curr[2];

| (24)

> | curr[N+1]:=4/3*curr[N]-1/3*curr[N-1];

| (25)

> | avecurr:=sum(curr[k],k=0..N+1)/(N+2);

| (26)

> | plot([seq([i*h,curr[i]/avecurr],i=0..N+1)],thickness=4,axes=boxed,style=point,labels=[x,'i/iavg']);

> |