Making an MPEG Film with Euler Math Toolbox

I once showed how to do an animated GIF with EMT. But it is also possible to make a film using the same method.

>function nicesave () ...
$  n=10; x=0:n; y=x'; r=0.5; m=50;
$  k=1;
$  loop 1 to 5
$     for a=(0:m-1)/m*2pi;
$       d=a+(x+y)/n*2*pi;
$       plot2d(x+r*cos(d),y+r*sin(d),>points,<grid,style="o#");
$       savepng("file"+printf("%03g",k)+".png",500,500); k=k+1;
$       wait(0);
$     end;
$  end;
$  exec("convert","-quality 100 file* animate.mpeg");
$  exec("cmd","/c del *.png");
>exec("cmd","/c start animate.mpeg");

The command nicesave() will save the PNG files into the directory of the notebook, convert to a film (using convert of ImageMagick), and finally delete the files. The exec() calls the system viewer for MPEG films.

Here is another example. We simulate a 3-body problem in a planar case. The function for the differential equation then takes the time x and the coordinates of the three bodies in a vector y.

>function f3body (x,y,m1,m2,m3) ...
$    y1=y[1:2]; y2=y[3:4]; y3=y[5:6];
$    d21=(y2-y1)/norm(y2-y1)^3*m1*m2;
$    d31=(y3-y1)/norm(y3-y1)^3*m3*m1;
$    d32=(y3-y2)/norm(y3-y2)^3*m3*m2;
$    return y[7:12]|(d21+d31)/m1|(-d21+d32)/m2|(-d31-d32)/m3;

The following functions solves the differential equation step by step with an adaptive Runge method. It plots the positions of the bodies after each step, and saves the plots into 720×1080 PNG files.

>function test3body (y1,m1,m2,m3,tmax,h,n=100,r=2,ms=4,f=16/9) ...
$    y=y1;
$    x=0;
$    markerstyle("o#");
$    k=1;
$    repeat;
$        ynew=adaptiverunge("f3body", ..
$            linspace(x,x+h,n),y,0.0001,h;m1,m2,m3);
$        H=ynew;
$        hold on;
$        clg;
$        cx=(H[1,-1]*m1+H[3,-1]*m2+H[5,-1]*m3)/(m1+m2+m3);
$        cy=(H[2,-1]*m1+H[4,-1]*m2+H[6,-1]*m3)/(m1+m2+m3);
$        setplot(cx-r*f,cx+r*f,cy-r,cy+r);
$        color(12); markersize(m1^(1/3)*ms); mark(H[1,-1],H[2,-1]);
$        color(13); markersize(m2^(1/3)*ms); mark(H[3,-1],H[4,-1]);
$        color(14); markersize(m3^(1/3)*ms); mark(H[5,-1],H[6,-1]);
$        hold off;
$        wait(h);
$        y=ynew[:,-1]'; x=x+h;
$        savepng("file"+printf("%04g",k)+".png",720*f,720); k=k+1;
$        until testkey;
$        until x>tmax;
$    end;
$    return x

With the following commands we make a film from the PNG files.

>exec("convert","-quality 100 file* film.mpeg");
>exec("cmd","/c del *.png");
>exec("cmd","/c start film.mpeg");

Note that the bodies are not to scale. To be visible they are larger. So they do not really collide as in the video. Moreover, the different marker sizes in savepng() are possible only in the upcoming version of EMT. If you try this, you will get bodies of the same size in the video.

The process is on the borderline of my computer. Converting takes a few minutes. Without the PNG saving, the animation is rather fast in EMT, however.

The film is on Youtube in the channel for Euler Math Toolbox.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.