# 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[[1]][[1]][[3]][[1]],
y /. sol[[1]][[1]][[3]][[2]]


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[[1]][[1]][[3]][[1]]


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[[1]][[1]][[3]][[1]] 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[357]:= s =
NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]

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

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

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

In[365]:= Clear[s];

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

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

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

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

In[371]:= s

Out[371]= {{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[[1]][[1]][[3]][[1]]


as requested.