Group Abstract Group Abstract

Message Boards Message Boards

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

GROUPS:
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}]
{{y->InterpolatingFunction[{{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
Answer
8 months ago
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
Answer
8 months ago
Great! they work well! 
POSTED BY: Shenghui Yang
Answer
8 months ago
Beware!

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
Answer
8 months ago
@Sean, this is true. I know that the .mx build is ususually not supported among different versions. 
POSTED BY: Shenghui Yang
Answer
8 months ago