Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to get data from solvin' numerically differential

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104146] Re: How to get data from solvin' numerically differential
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 21 Oct 2009 06:28:58 -0400 (EDT)
  • References: <200910200852.EAA06203@smc.vnet.net>

Hi Mikhail,

The easiest probably is to use Put to store the definition
(InterpolatingFunction object), and later Get to load it into another
Mathematica session. I use it all the time and it works really well for me.

Here is a working example which will likely work on your machine without any
modification (provided that you have the administrator rights - you may want
to alter the base directory through the BaseDirectory option, I set it to
$UserBaseDirectory by default). The code below makes a few utility functions
to conveniently work with temporary (test) directories

Unprotect[BaseDirectory];
ClearAll[dirExistsQ, createTestDir, removeTestDir, getBaseDir,
  BaseDirectory];
Protect[BaseDirectory];

Options[dirExistsQ] = Options[createTestDir] = Options[removeTestDir] =
     Options[getBaseDir] = {BaseDirectory -> $UserBaseDirectory};

getBaseDir[OptionsPattern[]] :=
  If[# === $UserBaseDirectory,
     ToFileName[{#, "Applications"}],
     #] &[OptionValue[BaseDirectory]];

dirExistsQ[dirName_String, opts : OptionsPattern[]] :=
  Select[FileNames["*", {getBaseDir[opts]}],
    FileType[#] === Directory &&
      Last[FileNameSplit[#]] === dirName &] =!= {};

createTestDir::exists = "The directory `1` already exists";
createTestDir[dirName_String, opts : OptionsPattern[]] /; !
    dirExistsQ[dirName, opts] :=
  CreateDirectory[ToFileName[{getBaseDir[opts], dirName}]];
createTestDir[dirName_String, opts : OptionsPattern[]] :=
 (Message[createTestDir::exists, dirName];
  ToFileName[{getBaseDir[opts], dirName}])

removeTestDir::nodir = "No directory was found with the name `1`";
removeTestDir[dirName_String, opts : OptionsPattern[]] /;
   dirExistsQ[dirName, opts] :=
  DeleteDirectory[ToFileName[getBaseDir[opts], dirName],
   DeleteContents -> True];
removeTestDir[dirName_String, OptionsPattern[]] :=
  "never happens" /; Message[removeTestDir::nodir, dirName];

The example proper:

In[1]:=
f=Interpolation[{#,Sin[#]}&/@N[Range[0,2*Pi,2*Pi/100]]]

Out[1]= InterpolatingFunction[{{0.,6.28319}},<>]

In[2]:= Plot[f[x],{x,0,2Pi}]

Output suppressed

In[3] =
(* Create a test directory if it does not exist, and choose a test file name
*)
testdir = createTestDir["Test"];
testfile = "testFile.dat";
testfileName = ToFileName[testdir, testfile];

(* Save your definition *)
Put[f, testfileName];
Clear[f];

The following code models another Mathematica session (you may indeed
restart Mathematica but then you will have to re-run the In[3] code to
define testfileName, or indicate it by hand )

In[4] :=
ClearAll[g];
g = Get[testfileName]

Out[4] =
InterpolatingFunction[{{0.,6.28319}},<>]

In[5] = Plot[g[x], {x, 0, 2 Pi}]

Output suppressed

Remove the test directory if it is no longer needed

In[6] = removeTestDir["Test"]


Of course, normally you know the directory where you want to store your
files,
so you just use Put and Get. I posted the above code just to provide a
self-contained working example independent of a particular machine or
directory structure.

In some cases (but probably not in this one), particularly when you want to
save the definitions of a DownValues or SubValues-based functions (or
generally, some global definitions), using Save instead of Put may be more
appropriate.

Hope this helps.

Regards,
Leonid



2009/10/20 =D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB =D0=A8=D0=B0=D0=BB=D0=B0=D0=
=B3=D0=B8=D0=BD=D0=BE=D0=B2 <shalm89 at gmail.com>

> Hi,
> Mathematica solves numerically differential equation and makes
> interpolation. I can plot this interpolating function. Can I save all
> the points in a file or somehow get this data?
> Best regards, Mikhail Shalaginov.
>
>


  • Prev by Date: Re: Re: Suggestions on how to use standard engineering symbols
  • Next by Date: Re: options for VectorPlot in version 7 vs
  • Previous by thread: Re: How to get data from solvin' numerically differential
  • Next by thread: Re: How to get data from solvin' numerically differential