4

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 say‎t=‎3‎. I use NDSolve as follows: ‎ enter image description here
The output is a vector of InterpolatingFunctions. 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 (Corei‏5‏ & ‎‏4‏GB 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.‎

Toughee
  • 113
  • 5

1 Answers1

5

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]

enter image description here

Related Q/As:

How to splice together several instances of InterpolatingFunction

Incompatible InterpolatingFunction between V9 and v10

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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 . – Toughee Sep 25 '14 at 02:56
  • Would you explain that why you've used the term : if1 = s[[1, -1, -1]]? I think I'm misusing this part of syntax. – Toughee Sep 25 '14 at 03:03
  • @Toughee, despite the typo (it was meant to be s[[1,1,-1]]) this syntax happens to work fine with a single function y. However, the correct form that works in general is s[[1,All,-1]]. It is an alternative to {x,y,z}/. s //First to get the right-hand-sides of Rules inside s. – kglr Sep 25 '14 at 03:17
  • 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"}. – bbgodfrey May 13 '18 at 13:57