Animating Laplace in Octave - jick.net · echo “file$variable ... So for creating mpeg movies, I...

2

Click here to load reader

Transcript of Animating Laplace in Octave - jick.net · echo “file$variable ... So for creating mpeg movies, I...

Page 1: Animating Laplace in Octave - jick.net · echo “file$variable ... So for creating mpeg movies, I set gnuplot to make .png image files instead ... matlab maybe, to make prettier

Animating Laplace in Octave

In this project, I mainly focussed on numerically solving the Poissonequation:

∆u =∂2u

∂x2+

∂2u

∂y2= f(x, y) (1)

using the following successive-over-relaxation algorithm:

Um,n ← Um,n+ωD−1

(fm,n −

Um+1,n − 2Um,n + Um−1,n

∆x2− Um,n+1 − 2Um,n + Um,n−1

∆y2

)(2)

Where D = − 2∆x2 − 2

∆y2 , where 0 < ω < 2 is an acceleration parameter,

causing (2) to converge faster.

My PDE professor, Anthony Pierce, gave this as an assignment to his MATH405 class, and gave it to me when I told him of my PHYS 210 project.

For my animations, I used Octave/Gnuplot for going through the algo-rithm (2) and plotting, the “convert” command of ImageMagick for makinganimated gif files, ffmpeg for making mpeg movies, and made a shell script tocall to these programs and change the necessary values for each consecutivegraph. I’m sure there are many other suitable programs, and I found manyPython scripts online as opposed to shell scripts. I encourage you to findbetter ways to get this done, especially since Gnuplot doesn’t have the mostbeautiful graphs.

The main purpose of the shell script was to change the name of the graphproduced on each iteration. The shell script then has the general form:

[FOR loop 0 → (# of graphs)]echo “file$variable(parameters)” > filerun$variable.mecho “matlab file: calculations→ save graph000$variable.gif” > file$variable.moctave filerun$variable.m[END FOR loop]convert -delay 10 -loop 0 graph*.gif laplace.gif

let me try to explain:We want to produce X # of graphs for our animation, so we want to produceX # of .m files containing the instructions to produce a unique graph, witha unique name (graph00X.gif). The third line is the generic .m file we areproducing, with $variable being the value of ”which number of graph” weare on. You place “> file$variable.m” at the very end of our generic .m file

1

Page 2: Animating Laplace in Octave - jick.net · echo “file$variable ... So for creating mpeg movies, I set gnuplot to make .png image files instead ... matlab maybe, to make prettier

to produce files called file001.m, file002.m.

Once we have all these .m files, we want our shell script to execute each ofthem automatically to produce the graphs. However, to do this, we have toopen Octave, and then type in the name of the function (file00X), along withany parameters it has. I got the shell script to produce a second set of .mfiles containing the instructions to run the “real” .m files named filerun00X.m(this is what the second line does). So once each pair of .m files are made,we run the filerun00X.m file to produce a graph (line 4).

Once all the graphs are made, we want to animate them. We use ImageMag-ick’s convert function to do this. “-delay” indicates the time interval betweeneach picture, and “-loop 0” indicates to loop the animation indefinitely. Con-vert is smart enough to know that graph*.gif means to animate all the filesnamed graph(something).gif in increasing numerical order (alphabetical toomaybe?). laplace.gif is the output file.

We can also make a movie file. I used ffmpeg to create an mpeg video;however, ffmpeg will not accept .gif files. So for creating mpeg movies, I setgnuplot to make .png image files instead (this might work with other imagetypes too). To make the animation, I replace “convert” code from line 6above with the following:

ffmpeg -i “graph%3d.png” -y “laplace.mpeg”

I looked into the movie option at the last minute, so I can only say that forsome reason %3d tells ffmpeg to take increasing numerical values for graphs,laplace.mpeg is the output video, and I don’t know what -y is.

Future Prospects

In the future I’d really like to make animations of time-dependent PDEs,rather than just rotating a Laplace/Poisson around. I saw that there isan algorithm called Crank-Nicholson for numerically solving the 1D heatequation. I coudn’t find an algorithm for the 2D heat equation though.

I would also like to try running the graphs to a different program, likematlab maybe, to make prettier graphs. It would also be nice to try to makea program in C, because calculations for a very fine mesh of a time-dependentPDE could get very complicated, and C would allow for a faster computation.But I believe in order to do that, one would have to do the calculations usingC, rather than calling to Octave/Matlab.

2