3

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?

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96

2 Answers2

6

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.

Edmund
  • 42,267
  • 3
  • 51
  • 143
4

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

LLlAMnYP
  • 11,486
  • 26
  • 65
  • 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. – Alexei Boulbitch 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. – LLlAMnYP Sep 18 '15 at 12:10
  • @AlexeiBoulbitch I have updated my answer to demonstrate better. – LLlAMnYP 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. – LLlAMnYP Sep 18 '15 at 12:20
  • Right, if I do not press "Show all", it works, thank you. – Alexei Boulbitch Sep 18 '15 at 12:28
  • Thanks for the accept, though I have to concede, ciao's comment is the cleaner and more programmatic way of doing this. – LLlAMnYP Sep 18 '15 at 12:30
  • LLlAMnYP Yes, I like ciao's approach also. One cannot accept two answers though. Besides, I more readily keep all the data within the same notebook in a separate InitializationCell, such that I could initialize all without thinking much of where did I store all that. This is private taste, however. – Alexei Boulbitch Sep 18 '15 at 12:39