Tuesday, August 10, 2010

Mathematica: import and export calculations

I often do long calculations in Mathematica. If I've got the time, I'll keep the kernel active and do all the fun plotting, animating, basic playing-around with the results right when I'm thinking about it. However, often I need to quit and move on to other things on my todo list. That's what this post is all about.

.m files

Any result in Mathematica can be saved to a .m file. The syntax is pretty basic:

sol=First[NDSolve[{y''[t]==-y[t]-beta y'[t], y[0]==1, y'[0]==0}, y, {t,0,10}]];
Export["filepath . . ./cool.m", sol]

Then later you can import the file like so:

solnew=Import["same m file location"];
Plot[y[t]/.solnew, {t,0,10}]

Note how I saved the result of an NDSolve command and later was able to use it to make a plot.


web storage

What's extra cool is that you can save your .m files to a web server because you can do things like this:

coolthing=Import["http://euclid.hamline.edu/~arundquist/research/hartree/listofsolsHLi.m"]

and be able to use results of calculations anywhere. I especially like this when I'm going to work on a project both at home and at work as the import command syntax doesn't need to change. I used to do this with Dropbox where both my machines would have an up-to-date copy of the .m files but I still had to change the syntax because my dropbox folder tends to be in different locations on different machines.

No comments:

Post a Comment