3
$\begingroup$

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?

$\endgroup$
2
  • 3
    $\begingroup$ Why not just DumpSave the symbol, Get where needed? $\endgroup$
    – ciao
    Commented Sep 18, 2015 at 10:37
  • $\begingroup$ Thank you, this works. Why should not you formulate this as a regular answer? $\endgroup$ Commented Sep 18, 2015 at 12:19

2 Answers 2

4
$\begingroup$

How about adding these lines of code below?

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.

notebook

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$ Commented Sep 18, 2015 at 12:00
  • $\begingroup$ @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. $\endgroup$
    – LLlAMnYP
    Commented Sep 18, 2015 at 12:10
  • $\begingroup$ @AlexeiBoulbitch I have updated my answer to demonstrate better. $\endgroup$
    – LLlAMnYP
    Commented Sep 18, 2015 at 12:14
  • $\begingroup$ BTW, when I click "Show all" my machine does take a few moments to think, but then outputs everything gracefully. $\endgroup$
    – LLlAMnYP
    Commented Sep 18, 2015 at 12:20
  • $\begingroup$ Right, if I do not press "Show all", it works, thank you. $\endgroup$ Commented Sep 18, 2015 at 12:28
6
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ This is quite neat. $\endgroup$
    – Michael E2
    Commented Sep 19, 2015 at 1:15
  • $\begingroup$ @Edmund Thank you. Do I understand correctly that the function is saved somewhere in the actual notebook? $\endgroup$ Commented Sep 21, 2015 at 8:30
  • $\begingroup$ @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. $\endgroup$
    – Edmund
    Commented Sep 21, 2015 at 9:07

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