# Exporting and Importing solution sets from NSolve

I have a rather large data set that I need to solve for, then save to my hard disk and import in the same format at a later time. This proves to be tricky. I use

sol =
Table[NSolve[{D[F[a, b, x, y], x] == 0, D[F[a, b, x, y], y] == 0}, {x, y}],
{a, 1, 10, 1}, {b, 1, 10, 1}]


for some given function F[a,b,x,y] which generates a 10 by 10 grid of all of the different solutions. There might not be the same number of solutions for a given $a$ and $b$ as another $a$ and $b$, for example F[a,b,x,y] is a $n^\mathrm{th}$ order polynomial. I then access the solutions with

x /. sol[][][][],
y /. sol[][][][]


where the $3^\mathrm{rd}$ solution for $a = 1$ and $b = 1$ is the one I want for example.

I've tried Export["/home/sol.dat",sol, "Data"] and Export["/home/sol.dat",sol,"Table"]. and then import with solimport = Import["/home/sol.dat", "Table"]

solimport[][][][]


then just gives me junk. What am I doing wrong here? I've tried .dat, .csv, and .txt and all seem to not preserve the form that Mathematica originally has. Note that the original dimensions aren't preserved in the imported data. If I use

"Format" = "CSV"


then dimension is preserved but solimport[][][][] still does not properly call the solutions for $x$ and $y$

Thanks!

You should use either Put or Save depending on what you want. With Put you just save the values of your expression. With Save you save the definition of the symbol. To retrieve the solutions you use Get. See code below

In:= s =
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y == 1}, y, {x, 0, 30}]

Out= {{y -> InterpolatingFunction[{{0., 30.}}, <>]}}

In:= Put[s, FileNameJoin[{$TemporaryDirectory, "s"}]] In:= Clear[s] In:= s = Get[FileNameJoin[{$TemporaryDirectory, "s"}]]

Out= {{y -> InterpolatingFunction[{{0., 30.}}, <>]}}

In:= Clear[s];

In:= s =
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y == 1}, y, {x, 0, 30}]

Out= {{y -> InterpolatingFunction[{{0., 30.}}, <>]}}

In:= Save[FileNameJoin[{$TemporaryDirectory, "s"}], s] In:= Clear[s] In:= Get[FileNameJoin[{$TemporaryDirectory, "s"}]]

Out= {{y -> InterpolatingFunction[{{0., 30.}}, <>]}}

In:= s

Out= {{y -> InterpolatingFunction[{{0., 30.}}, <>]}}


I actually found to use

Export["/home/sol.csv", sol, "CSV"]


and

solImport =
Import["/home/sol.csv",
"CSV"];


with

solimport = solImport // ToExpression


to fix the problem of table contraction. The user can now call indexed entries at

solimport[][][][]


as requested.