How to store an InterpolatingFunction? [duplicate]

Assume I have obtained an InterpolatingFunction, say, as the result of solving of a differential equation, say, this one:

sol = First[
NDSolve[{D[u[t, x, y],
t] == 0.075  (D[u[t, x, y], x, x] + D[u[t, x, y], y, y]) -
u[t, x, y] (2 D[u[t, x, y], x] -  D[u[t, x, y], y]),
u[0, x, y] == Exp[-(x^2 + y^2)], u[t, -4, y] == u[t, 4, y],
u[t, x, -4] == u[t, x, 4]}, u, {t, 0, 2}, {x, -4, 4}, {y, -4, 4}]]

(it is simply an example taken from a tutorial, just to have something to discuss).

I would like to store this function in the notebook, such that I can get this interpolating function upon reopening of the notebook without solving this equation once more.

Indeed, a real-world equation may be much more complex and may require much more time, than this one. A further work with the solution without resolving the equation would be of advantage.

Any ideas?

• Why not just DumpSave the symbol, Get where needed?
– ciao
Sep 18 '15 at 10:37
• Thank you, this works. Why should not you formulate this as a regular answer? Sep 18 '15 at 12:19

u /. %;
FullForm @ %

Then saving the notebook. Once reopened, the FullForm will be present as an output cell and you can type in u = right in front of that cell and input that.

UPDATE

Screenshot of what happens after I close the notebook, then ClearAll[sol, u], then reopen it again and carry out my suggested steps. Clicking "show all" isn't necessary. • Thank you, unfortunately when I do this I get a "Large output" message, and after I press the button "Show all" the hang-up follows. Sep 18 '15 at 12:00
• @AlexeiBoulbitch I actually didn't even bother pressing "Show all". Doing all the other steps I've listed, I then simply typed `Plot3D[u[1,x,y],{x,-4,4},{y,-4,4}] and everything plotted fine. Sep 18 '15 at 12:10
• @AlexeiBoulbitch I have updated my answer to demonstrate better. Sep 18 '15 at 12:14
• BTW, when I click "Show all" my machine does take a few moments to think, but then outputs everything gracefully. Sep 18 '15 at 12:20
• Right, if I do not press "Show all", it works, thank you. Sep 18 '15 at 12:28

You can also use LocalSymbol (version 10.2 and above) to store the result in the local file system for retrieval in any notebook even after a kernel restart. Add the following:

LocalSymbol["MySolution"] = sol;

"MySolution" can be any string. Now the value of sol is stored in the file system. To retrieve its value later use:

LocalSymbol["MySolution"]

The value of sol is returned.

Hope this helps.

• This is quite neat. Sep 19 '15 at 1:15
• @Edmund Thank you. Do I understand correctly that the function is saved somewhere in the actual notebook? Sep 21 '15 at 8:30
• @AlexeiBoulbitch No, it is not stored in the notebook but in the directory returned by \$LocalSymbolBase. Because it is in a directory then any notebook can access it even after restarting Mathematica. Sep 21 '15 at 9:07