I am going to develop an unsteady diffusion calculation in Mathematica. Because it is unsteady, I would like to visualize the solution each time step (or in some time steps). My MATLAB skills are much better than Mathematica, so I am going to give the example from there. I looked at some of the posts about that, but they were for small number of time steps like 10, or saving each plot and creating a movie. But in MATLAB I can do


x and z are the position vectors and U_sol is the solution matrix. GetFrame simply preserves frame but not the plot, so it looks like a continuous plotting, like a movie.

In Matlab, you can do something like this to save the movie directly. This is before time steps


and inside the time loop


and at the end of the time loop.

 close (writerObj)

and you will have the movie file without dealing with each frame. There should be some equivalent version in Mathematica.

  • $\begingroup$ I guess you want ListPlot3D[] (or Plot3D[] if you're using NDSolve[] for the PDE), Animate[], and/or Export[]... $\endgroup$ – J. M. will be back soon Dec 27 '16 at 13:16
  • $\begingroup$ mathematica can not Export on a frame by frame basis, you need to compile all frames as a list and Export all at once. $\endgroup$ – george2079 Dec 27 '16 at 13:48
  • $\begingroup$ I am not using NDSolve. I use my own code and every time step I create a solution which I want continuously plot without recreating the frame and ideally save them while plotting them. But it seems I need to save each frame but that is really inefficient if you have 10000 or more steps. $\endgroup$ – Erdem Dec 27 '16 at 17:13
  • $\begingroup$ if you want to watch the frames as the solution proceeds have a look at Monitor $\endgroup$ – george2079 Dec 27 '16 at 20:25
  • $\begingroup$ If you are on Windows you can use my MathMF package to write frames one by one to a video stream. $\endgroup$ – Simon Woods Dec 27 '16 at 22:24

Mathematica can't build a writer object (in fact it doesn't have proper objects at all, excluding the front-end) but you can always build your snapshots in a temporary directory. Give this a go:

movieDir = CreateDirectory[];
getFrame[plot_, tag_] := 
  Export[FileNameJoin@{movieDir, ToString[tag] <> ".png"}, plot];
getImages[] := 
  Import /@ 
   SortBy[FileNames["*.png", movieDir], ToExpression@FileBaseName@# &];
createMovie[file_] :=
  With[{images = getImages[]},
   With[{dims = ImageDimensions@First@images},
     ImageResize[ColorConvert[#, "RGB"], dims] & /@ images

Note the manipulations done to the images in createMovie. That's to get around the issues mentioned here.

Then we can build our plots and images:

In[458]:= (Do[
   getFrame[Plot[Sin[s*x], {x, 0, 2 \[Pi]}, PlotRange -> {-1, 1}], 
   {s, 0, 2, .1}
  createMovie["~/Desktop/sol.gif"]) // AbsoluteTiming

Out[458]= {3.2397, "~/Desktop/sol.gif"}

And what we get is this (note that .avi works too, I just used .gif so I could embed it here):

visualization gif

  • $\begingroup$ Thank you ..this is what I was trying to do. $\endgroup$ – Erdem Dec 28 '16 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.