Re: NDSolve

*To*: mathgroup at smc.vnet.net*Subject*: [mg13870] Re: NDSolve*From*: Robert Knapp <rknapp>*Date*: Wed, 2 Sep 1998 01:31:03 -0400*Organization*: Wolfram Research, Inc.*References*: <6s5ma3$cbq@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Jim McGuire wrote: > > I have been using NDSolve to successfully solve some differential > equations and would like to export the interpolating functions to some > compiled C/Fortran code. Is there a straightforward way of doing this? An InterpolatingFunction is an object which encapsulates the data produced by the ODE solver run by NDSolve, and it is quite simple to export the data. However, what would be very difficult to export would be the ability to evaluate the InterpolatingFunction object at an argument -- i.e. its role as an approximate function. (The code for evaluating these is of course based on piecewise polynomial interpolation methods and is moderately complex.) If you want the data, the third part of an InterpolatingFunction object is the grid (or knots) at which data values are defined, and the fourth part is the actual data (possibly including derivatives). Here is a simple example: In[1]:= fun = x /. First[NDSolve[{x'[t] == x[t],x[0] == 1},x,{t,0,.1}]] Out[1]= InterpolatingFunction[{{0., 0.1}}, <>] These are the points at which data is defined: In[2]:= fun[[3]] Out[2]= {{0., 0.0000998752, 0.00019975, 0.0207957, 0.0413916, 0.0619875, 0.1}} This is enclosed in an extra {} because an InterpolatingFunction object may be multidimensional -- then outer {} contains the list of values for each dimension. This gives the data (including derivative values) corresponding to the points above In[3]:= fun[[4]] Out[3]= {{1.}, {1.0001, 1.0001}, {1.0002, 1.0002, 0.5001}, > {1.02101, 1.02101, 0.505303}, {1.04226, 1.04226, 0.515818}, > {1.06395, 1.06395, 0.526553, 0.173737}, > {1.10517, 1.10517, 0.554975, 0.224068}} The data is arranged in lists of increasing order, and derivatives are divided by the factorial of the order. Thus, at t == 0.1, we have {f, f', f''/2!, f'''/3!} == {1.10517, 1.10517, 0.554975, 0.224068} Be warned, however, that the internal format of data objects like this may change from version to version. In the next release of Mathemematica that will be coming out there will be a change in the format of the data to take advantage of more compact storage, but with only slightly more effort, you could still get this information. Rob Knapp Wolfram Research