In comments to my previous question it was suggested to extract all the data from InterpolatingFunction generated by Mathematica 5.2 and then create another one in Mathematica 8. The suggested approach is to use functions defined in the DifferentialEquations`InterpolatingFunctionAnatomy` package for extraction of the data from the InterpolatingFunction. Trying it naively,
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"]
ifun = First[
x /. NDSolve[{x'[t] == Exp[x[t]] - x[t], x[0] == 1},
x, {t, 0, 10}]];
data = Transpose@{InterpolatingFunctionGrid[ifun],
InterpolatingFunctionValuesOnGrid[ifun]};
interpolationOrder =
Developer`FromPackedArray@
InterpolatingFunctionInterpolationOrder[ifun];
ifun2 = Interpolation[data, InterpolationOrder -> interpolationOrder];
Table[ifun[x] - ifun2[x], {x, 0, 0.5160191740198963`, .1}]
I get significant difference between original function and reconstructed one:
{0., 2.13061*10^-7, 2.05087*10^-7, 2.46198*10^-7, 6.68937*10^-7,
1.5624*10^-7}
Looking at InputForm of these functions reveals that they are not identical. Is it possible to reconstruct the InterpolatingFunction through extraction of all the data from it and calling Interpolation on the extracted data?
EDIT
Here is a general solution, in code:
Please see below for the explanation. Note however that the above code does rely on some details of the
InterpolatingFunctionobject which may be version-specific, since apparently the API ofDifferentialEquations`InterpolatingFunctionAnatomy`does not seem to allow to fully reconstruct the original object when values for function derivatives are important.End EDIT
It appears that
NDSolveincludes the information on derivatives when constructing theInterpolatingFunction, which makes sense. In your case, that would include the first derivative, since your equation is first – order. But, this information is lost when we use the functions from theDifferentialEquations`package. The way to get it is to access the initialInterpolatingFunctionAnatomy`
InterpolatingFunctionobject directly. Here is a simple example:This suggests that each value is followed by the value of the derivative, at this grid point. Therefore, the way to construct a new object is something like this:
This uses the extended form of
Interpolation, where one can also specify the values for derivatives. This passes our test:The way to determine, up to which derivative do we have the information in a given
InterpolatingFunction, is to look at this part:In this case, the step is 2, so we have a value plus a first derivative. For, say, a second-order equation, the step will be 3, and you will need
Partition[...,3]. So, you determine the order by getting the step in this part.Now, the real thing: