Message Boards Message Boards

4 Replies
4 Total Likes
View groups...
Share this post:

Is there a way to store the InterpolatingFunction object in a file?

Posted 11 years ago
My question is that if we can place the InterpolatingFunction object into a file so I can use it directly next time even if I have quit kernel? For instance, I solve this function numerically, 
s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]
Can I call the result of "y" without recompute differential equation? Of course I can export point value of y to some file and save it later. I am looking forward to a neater way to handle this . 

Any idea is appreciated. 
POSTED BY: Shenghui Yang
4 Replies

The internal structure of InterpolatingFunction statements is not documented and may not be consistent between versions of Mathematica. That is, it not always possible to use an InterpolationFunction expression created in one version of Mathematica inside another version.
POSTED BY: Sean Clarke
@Sean, this is true. I know that the .mx build is ususually not supported among different versions. 
POSTED BY: Shenghui Yang
Great! they work well! 
POSTED BY: Shenghui Yang
1) Simplest saving to a file

s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}];
Export["test.m", s];
Saving to .m format will completely preserve the information. Importing it back will work as original one:
saved = Import["test.m"]
Plot[Evaluate[y[x] /. saved], {x, 0, 30}, PlotRange -> All]

2) Saving in the notebook

You can export to a compressed string via Compress and save that string in the notebook. Then Uncompress will give you the original result.

3) More extensive discussion

How do I save a variable or function definition to a file?
POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract