16.12.2012 Views

Computer Algebra Recipes

Computer Algebra Recipes

Computer Algebra Recipes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6.3. NUMERICAL SIMULATION OF PDES 283<br />

> f:=(i,j)->2*(1+r)*V[i,j]-V[i+1,j]-V[i-1,j]-r*(V[i,j+1]<br />

+V[i,j-1])+h^2*x[i]*exp(y[j])=0;<br />

f := (i; j) ! 2(1+r) Vi; j ¡ Vi+1;j ¡ Vi¡1;j ¡ r (Vi; j+1 + Vi; j¡1)+h 2 xi e yj<br />

The mesh equations are determined for all the internal grid points using two<br />

nested sequences running from i =1tom ¡ 1andfromj =1ton ¡ 1.<br />

> eqs:=fseq(seq(f(i,j),i=1..m-1),j=1..n-1)g:<br />

The unknown potentials at the internal grid points are entered as the variables.<br />

> vars:=fseq(seq(V[i,j],i=1..m-1),j=1..n-1)g:<br />

The mesh equations are solved (to 6 digits) for the variables, and the solution<br />

is assigned.<br />

> sol:=evalf(fsolve(eqs,vars),6): assign(sol):<br />

Three-dimensional plotting points are formed for all the grid points, including<br />

those on the boundaries.<br />

> pts:=seq(seq([x[i],y[j],V[i,j]],i=0..m),j=0..n):<br />

Using the pointplot3d command, the points representing the potential at each<br />

grid point are plotted as size-6 blue circles, the result being shown in Figure 6.4.<br />

> pointplot3d([pts],symbol=circle,symbolsize=6,color=blue,<br />

axes=boxed,orientation=[135,60],tickmarks=[3,3,3],<br />

labels=["x","y","V"]);<br />

4<br />

V<br />

2<br />

0<br />

2<br />

1<br />

x<br />

0<br />

0.5 y<br />

Figure 6.4: Numerical solution of the Poisson equation.<br />

The CPU time to execute the entire recipe is about 6 seconds.<br />

> cpu:=time()-begin;<br />

cpu := 5:978<br />

0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!