16.12.2012 Views

Computer Algebra Recipes

Computer Algebra Recipes

Computer Algebra Recipes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.3. NUMERICAL SIMULATION OF PDES 277<br />

evaluates the time step size k = rh 2 ,aswellastheparameterc. Wanting to<br />

animate his numerical solution, he decides to create N = 50 plots, with each<br />

plotseparatedintimebys =20steps.<br />

> r:=0.05: h:=1.0/(M+1); k:=r*h^2; c:=1-2*r; N:=50: s:=20:<br />

h := 0:08333333333 k := 0:0003472222222 c := 0:90<br />

An operator f will be used to input the initial parabolic temperature pro¯le.<br />

> f:=x->evalf(100*x*(1-x)):<br />

With the help of f, the initial (j = 0 time row) temperatures at the M internal<br />

mesh points are calculated, and expressed as a column vector4 T0 so that matrix<br />

multiplication can be performed.<br />

> T[0]:=; #input temperatures<br />

2<br />

6<br />

T0 := 6<br />

4<br />

7:638888889<br />

13:88888889<br />

18:75000000<br />

22:22222222<br />

24:30555556<br />

25:00000000<br />

24:30555556<br />

22:22222222<br />

18:75000000<br />

13:88888889<br />

7:638888886<br />

A graphing operator is formed to plot the temperature pro¯le on the jth step,<br />

the plotting points being connected by straight lines. The zero temperatures at<br />

the endpoints are included.<br />

> gr:=j->plot([[0,0],seq([i/(M+1),T[j][i,1]],i=1..M),[1,0]],<br />

labels=["x","T"]):<br />

In the following do loop, the temperature pro¯le is calculated every s=20steps. > for n from 1 to N do<br />

> T[n]:=A^s . T[n-1];<br />

> end do:<br />

The CPU time on a 3-GHz personal computer to perform the calculation<br />

> CPUtime:=(time()-begin)*seconds;<br />

CPUtime := 0:350 seconds<br />

is a fraction of a second, hardly worth recording. The plots[display] command<br />

5 with the insequence=true option is employed to produce an animated<br />

sequence of the temperature pro¯le in the rod.<br />

> plots[display]([seq(gr(j),j=0..N)],insequence=true);<br />

4 Entered with the short-hand notation >.<br />

5 Ashortcutto¯rstenteringwith(plots) and then display.<br />

3<br />

7<br />

5

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

Saved successfully!

Ooh no, something went wrong!