Home > Archive > Mathematica > May 2006 > Reconstructing data points from a InterpolatingFunction object
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Reconstructing data points from a InterpolatingFunction object
|
|
| Eckhard Schlemm 2006-05-15, 10:05 pm |
| Hello everyone,
My question is probably rather simple to answer but I just don't know how to
do it.
I am nummerically solving a differential equation using NDSolve and then I
want to apply a NonlinearFit on the resulting InterpolatingFunction
object....and therefore need to have access to the data points which NDSolve
created and from which the InterpolatingFunction is created....Is there any
fast and direct way to get these data points ? (There must be....)
Thanks in advance
Eckhard
| |
| Andrzej Kozlowski 2006-05-17, 4:13 am |
| On 16 May 2006, at 12:49, Eckhard Schlemm wrote:
> Hello everyone,
>
> My question is probably rather simple to answer but I just don't
> know how to
> do it.
>
> I am nummerically solving a differential equation using NDSolve and
> then I
> want to apply a NonlinearFit on the resulting InterpolatingFunction
> object....and therefore need to have access to the data points
> which NDSolve
> created and from which the InterpolatingFunction is created....Is
> there any
> fast and direct way to get these data points ? (There must be....)
>
> Thanks in advance
>
> Eckhard
>
>
Indeed, the data points can be got out from any
InterpolatingFunction, but the most direct method to do so depends on
which version of Mathematica you are using. You can see it on an
example. I Mathematica 5.1:
ls = {{0, .1}, {1, .2}, {2, .3}, {3, .4}};
f = Interpolation[ls, InterpolationOrder -> 1];
f//InputForm
InterpolatingFunction[{{0., 3.}}, {2, 0, True, Real, {1},
{0}}, {{0., 1., 2., 3.}}, {{0, 1, 2, 3, 4},
{0.1, 0.2, 0.3, 0.4}}, {Automatic}]
Hence your data list is
f[[-2]]
{{0,1,2,3,4},{0.1,0.2,0.3,0.4}}
On the other hand in Mathematica 4.1 the same code returns
InterpolatingFunction[{{0., 3.}}, {1, 0, True, Real, {1},
{0}}, {{0., 1., 2., 3.}}, {{0, 1, 2, 3, 4},
{0.1, 0.2, 0.3, 0.4}}]
So you can get the data with f[[-1]].
Andrzej Kozlowski
| |
|
|
Hi Eckhard ,
you can look at the InterpolatingFunction object f, by:
InputForm[f]
and you will see that you can get the data points by:
f[[3]]
Daniel
Eckhard Schlemm wrote:
> Hello everyone,
>
> My question is probably rather simple to answer but I just don't know how to
> do it.
>
> I am nummerically solving a differential equation using NDSolve and then I
> want to apply a NonlinearFit on the resulting InterpolatingFunction
> object....and therefore need to have access to the data points which NDSolve
> created and from which the InterpolatingFunction is created....Is there any
> fast and direct way to get these data points ? (There must be....)
>
> Thanks in advance
>
> Eckhard
>
>
| |
| Jens-Peer Kuska 2006-05-17, 4:13 am |
| Hi,
look what the
Needs[" DifferentialEquations`InterpolatingFunct
ionAnatomy`"]
can do for you.
Regards
Jens
"Eckhard Schlemm" <e.schlemm@hotmail.de> schrieb
im Newsbeitrag news:e4bits$1a9$1@smc.vnet.net...
| Hello everyone,
|
| My question is probably rather simple to answer
but I just don't know how to
| do it.
|
| I am nummerically solving a differential
equation using NDSolve and then I
| want to apply a NonlinearFit on the resulting
InterpolatingFunction
| object....and therefore need to have access to
the data points which NDSolve
| created and from which the InterpolatingFunction
is created....Is there any
| fast and direct way to get these data points ?
(There must be....)
|
| Thanks in advance
|
| Eckhard
|
|
| |
| Peter Pein 2006-05-17, 4:13 am |
| Eckhard Schlemm schrieb:
> Hello everyone,
>
> My question is probably rather simple to answer but I just don't know how to
> do it.
>
> I am nummerically solving a differential equation using NDSolve and then I
> want to apply a NonlinearFit on the resulting InterpolatingFunction
> object....and therefore need to have access to the data points which NDSolve
> created and from which the InterpolatingFunction is created....Is there any
> fast and direct way to get these data points ? (There must be....)
>
> Thanks in advance
>
> Eckhard
>
>
Hi Eckhard,
sol=y/.First@
NDSolve[{y''[x]==- y[x]+y'[x]Cos[x]+Sin[3x],y[0]==0,y'[0]==
0},y,{x,0,10Pi}];
xyData=Transpose[{sol[[3,1]],sol[[4,2,1+
Most@sol[[4,1]]]]}];
and compare
p1=ListPlot[xyData,PlotJoined->True];
to
p2 = Plot[
sol[x], {x, 0, 10Pi}, PlotPoints -> Length[sol[[4, 1]]] - 1, PlotStyle -> Red];
DisplayTogether[p1,p2]
Peter
| |
| bghiggins@ucdavis.edu 2006-05-17, 4:13 am |
| Eckhard,
You can use the DifferentialEquations`InterpolatingFunct
ionAnatomy` in
Mathematica 5.2. This package does not appear under standard packages
as it likely to change in future versions. It is located in
AddOns/ExtraPackages/DifferentialEquations. The tools in this package
allow one to maniputate data in InterpolationFunctions.
<< DifferentialEquations`InterpolatingFunct
ionAnatomy`
Consider the following ODE:
ifun = First[x /. NDSolve[{x'[t] == x[t]Cos[t], x[0] == 1}, x, {t, 0,
10}]]
Here ifun is the InterpolationFunction returned by NDSolve. The
functions you want to use are InterpolatingFunctionCoordinates and
InterpolatingFunctionValuesOnGrid. So, for eaxmple, if you want to plot
the discrete data you can proceed as follows:
tvalues=Flatten[InterpolatingFunctionCoo
rdinates[ifun]];
xvalues=InterpolatingFunctionValuesOnGri
d[ifun];
ListPlot[Transpose[{tvalues,xvalues}]]
Hope this helps,
Cheers,
Brian
| |
| Paul Abbott 2006-05-17, 4:13 am |
| In article <e4bits$1a9$1@smc.vnet.net>,
"Eckhard Schlemm" <e.schlemm@hotmail.de> wrote:
> I am nummerically solving a differential equation using NDSolve and then I
> want to apply a NonlinearFit on the resulting InterpolatingFunction
> object....
What do you want the NonlinearFit to do? Note that in Version 5 you can
use FindFit instead of NonlinearFit. Also, you can use
InterpolatingFunctions directly, rather than sampling or re-sampling
them.
I think a concrete example is likely to generate a more useful answer.
> and therefore need to have access to the data points which NDSolve
> created and from which the InterpolatingFunction is created....
I don't follow the use of therefore here. The data points at which
NDSolve samples the solution depends on the automatic and optional
settings supplied to NDSolve -- so I don't see why accessing these
points is required. You can, of course, just generate a Table of points
by re-sampling the InterpolatingFunction.
> Is there any fast and direct way to get these data points ?
> (There must be....)
See the Advanced Documentation for NDSolve. This can be opened directly
be executing the following command:
NotebookOpen[FrontEnd`FileName[
{$TopDirectory, "Documentation", "English","RefGuide",
"AdvancedDocumentation","DifferentialEquations"}, "Packages.nb"]]
Cheers,
Paul
________________________________________
_______________________________
Paul Abbott Phone: 61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul
| |
| Jean-Marc Gulliet 2006-05-17, 4:13 am |
| Eckhard Schlemm wrote:
> Hello everyone,
>
> My question is probably rather simple to answer but I just don't know how to
> do it.
>
> I am nummerically solving a differential equation using NDSolve and then I
> want to apply a NonlinearFit on the resulting InterpolatingFunction
> object....and therefore need to have access to the data points which NDSolve
> created and from which the InterpolatingFunction is created....Is there any
> fast and direct way to get these data points ? (There must be....)
>
> Thanks in advance
>
> Eckhard
>
>
Hi Eckhard,
Although you can always manipulate the result of NDSolve as any
expression (see In[1]), using the functions located in the package
"InterpolatingFunctionAnatomy" (see In[3] and In[4])should be easier and
better since they are version independent:
"[...] occasionally it is useful to access the data inside [of the
interpolating function], which includes the actual values and points
NDSolve computed when taking steps.
The exact structure of an InterpolatingFunction object is arranged to
make the data storage efficient and evaluation at a given point fast.
This structure may change between Mathematica versions, so code which is
written in terms of accessing parts of InterpolatingFunction objects may
not work with new versions of Mathematica.
The DifferentialEquations`InterpolatingFunct
ionAnatomy` package provides
an interface to the data in an InterpolatingFunction object which will
be maintained for future Mathematica versions [1]."
In[1]:=
sol = NDSolve[{Derivative[1][y][x] == y[x],
y[1] == 2}, y, {x, 0, 3}]
FullForm[sol[[1,1,2]]]
In[3]:=
Needs[
" DifferentialEquations`InterpolatingFunct
ionAnatomy`"]
In[4]:=
Names[
" DifferentialEquations`InterpolatingFunct
ionAnatomy`*"]
Out[4]=
{InterpolatingFunctionCoordinates,Interp
olatingFunctionDerivativeOrder,\
InterpolatingFunctionDomain,Interpolatin
gFunctionGrid,\
InterpolatingFunctionInterpolationOrder,
InterpolatingFunctionValuesOnGrid}
Best regards,
Jean-Marc
[1]
http://documents.wolfram.com/mathem...ionAnatomy.html
| |
| Paul Abbott 2006-05-20, 4:17 am |
| In article <e4jtte$d24$1@smc.vnet.net>,
"Eckhard Schlemm" <e.schlemm@hotmail.de> wrote:
> Thanks to your help - and the other answers of course - I managed to get
> hold of the data points created by NDSolve to sample the
> InterpolatingFunction..But still when I use NonlinearFit[] to fit a model to
> these data the outcome is somewhat strange...for a listplot of data and a
> plot of the "best fit function" clearly indicates that there are better
> paramters to fit the model to the data....I think this problem has nothing
> to do with from where I got the data points,,,
Correct.
> in my case from the InterpolatingFunction object but I don't think that
> is the cause of this strange behavior.
Correct.
> Can you tell me, why ff[t] is so much different from the data points of
> data?... Oh I almost forgot... it works properly for some values of a and
> T... for example (a,T)=(.5 , 30) seems to work.. but others do not....for
> instance (a,T)=(.7 , 30) yields a result which I cannot quite belief to be
> the best fit....
NonlinearFit and FindFit are both sensitive to the choice of starting
parameters. For a = 0.7, if you do
NonlinearFit[data, a Sin[b t + g], t, {{a, 0.7}, {b, 1/2}, {g, Pi/2}}]
you will get a much better fit.
The solution to the differential equation looks like a Jacobi elliptic
function.
Also, you are fitting to a trig function -- so why not use Fourier
methods to determine the amplitude and frequency?
Cheers,
Paul
________________________________________
_______________________________
Paul Abbott Phone: 61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul
|
|
|
|
|