The output of NDSolve
is a (list of)InterpolatingFunction
(s). In standard output format the elements of interpolation are not printed explicitly. I want to know if there is any way to access these values?
I need to obtain the value of the solution of an ODE system, at the endpoint of the interval, let's sayt=3
. I use NDSolve
as follows:
The output is a vector of InterpolatingFunction
s. When I put the answers vector in Psi[t]
(a vector of functions of t
) and then replace the variable t
by 3, it seems that Mathematica starts interpolating all functions and then replaces t=3
.
But for some large systems (more than 30000 x 30000). this procedure becomes unbearably time consuming or even impossible for a PC (Corei5 & 4GB of RAM) to perform. Thus, if both of the interpolating points and the corresponding values be available (at least at boundary points), they can be used instead, much more simply.
$\begingroup$
$\endgroup$
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
4
In version 9 you can use Part
to access the parts of an InterpolatingFunction
:
points = {{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}};
ifun = Interpolation[points]
(* InterpolatingFunction[{{0,5}},<>] *)
{ifun[[3]], ifun[[4]]}
(* {{{0,1,2,3,4,5}},{{0},{1},{3},{4},{3},{0}}} *)
You can also access Properties
of ifun
using (not ifun["Properties"]
as one would expect) ifun["Methods"]
:
ifun["Methods"]
(* {"Coordinates", "DerivativeOrder", "Domain", "ElementMesh",
"Evaluate", "Grid", "InterpolationOrder", "MethodInformation",
"Methods", "Properties", "ValuesOnGrid"} *)
{ifun["Coordinates"][[1]], ifun["ValuesOnGrid"]}
(* {{0,1,2,3,4,5}, {0,1,3,4,3,0}} *)
Using the above with NDSolve
s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]
(* {{y -> InterpolatingFunction[{{0.,30.}},<>]}} *)
if1 = s[[1, All, -1]]
(* InterpolatingFunction[{{0.,30.}},<>] *)
{if1["Coordinates"][[1]], if1["ValuesOnGrid"]} // Short
(* {{0.,0.00017069,0.00034138,<<332>>,29.7515,29.8757,30.},{<<1>>}}*)
coords = Transpose[{if1["Coordinates"][[1]], if1["ValuesOnGrid"]}];
ListPlot[coords]
Related Q/As:
How to splice together several instances of InterpolatingFunction
-
$\begingroup$ Thanks for your attention. I've tried it, but even for a partly small example that I've solved before in less than 5 seconds, this syntax caused to an immediate memory overflow! A note I should stated before is that I'm using Mathematica 8.0 . $\endgroup$– TougheeCommented Sep 25, 2014 at 2:56
-
$\begingroup$ Would you explain that why you've used the term : if1 = s[[1, -1, -1]]? I think I'm misusing this part of syntax. $\endgroup$– TougheeCommented Sep 25, 2014 at 3:03
-
$\begingroup$ @Toughee, despite the typo (it was meant to be
s[[1,1,-1]]
) this syntax happens to work fine with a single functiony
. However, the correct form that works in general iss[[1,All,-1]]
. It is an alternative to{x,y,z}/. s //First
to get the right-hand-sides ofRule
s insides
. $\endgroup$– kglrCommented Sep 25, 2014 at 3:17 -
$\begingroup$ By v11.3,
ifun["Methods"]
has grown to{"Coordinates", "DerivativeOrder", "Domain", "ElementMesh", "Evaluate", "GetPolynomial", "Grid", "InterpolationMethod", "InterpolationOrder", "MethodInformation", "Methods", "OutputDimensions", "Periodicity", "PlottableQ", "Properties", "QuantityUnits", "Unpack", "ValuesOnGrid"}
. $\endgroup$ Commented May 13, 2018 at 13:57