Utility Packages for Numerical Differential Equation Solving

InterpolatingFunctionAnatomy

NDSolve returns solutions as InterpolatingFunction objects. Most of the time, simply using these as functions does what is needed, but occasionally it is useful to access the data inside, 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 Wolfram Language versions, so code that is written in terms of accessing parts of InterpolatingFunction objects may not work with new versions of the Wolfram Language. The package provides an interface to the data in an InterpolatingFunction object that will be maintained for future Wolfram Language versions.

InterpolatingFunctionDomain[ifun]return a list with the domain of definition for each of the dimensions of the InterpolatingFunction object ifun
InterpolatingFunctionCoordinates[ifun]return a list with the coordinates at which data is specified in each of the dimensions for the InterpolatingFunction object ifun
InterpolatingFunctionGrid[ifun]return the grid of points at which data is specified for the InterpolatingFunction object ifun
InterpolatingFunctionValuesOnGrid[ifun]return the values that would be returned by evaluating the InterpolatingFunction object ifun at each of its grid points
InterpolatingFunctionInterpolationOrder[ifun]return the interpolation order used for each of the dimensions for the InterpolatingFunction object ifun
InterpolatingFunctionDerivativeOrder[ifun]return the order of the derivative of the base function for which values are specified when evaluating the InterpolatingFunction object ifun

Anatomy of InterpolatingFunction objects.

This loads the package.
In[1]:=
Click for copyable input

One common situation where the package is useful is when NDSolve cannot compute a solution over the full range of values that you specified, and you want to plot all of the solution that was computed to try to understand better what might have gone wrong.

Here is an example of a differential equation which cannot be computed up to the specified endpoint.
In[2]:=
Click for copyable input
Out[2]=
This gets the domain.
In[3]:=
Click for copyable input
Out[3]=
Once the domain has been returned in a list, it is easy to use Part to get the desired endpoints and make the plot.
In[4]:=
Click for copyable input
Out[5]=

From the plot, it is quite apparent that a singularity has formed and it will not be possible to integrate the system any further.

Sometimes it is useful to see where NDSolve took steps. Getting the coordinates is useful for doing this.

This shows the values that NDSolve computed at each step it took. It is quite apparent from this that nearly all of the steps were used to try to resolve the singularity.
In[6]:=
Click for copyable input
Out[7]=

The package is particularly useful for analyzing the computed solutions of PDEs.

With this initial condition, Burgers' equation forms a steep front.
In[8]:=
Click for copyable input
Out[8]=
This shows the number of points used in each dimension.
In[9]:=
Click for copyable input
Out[9]=
This shows the interpolation order used in each dimension.
In[10]:=
Click for copyable input
Out[10]=
This shows that the inability to resolve the front has manifested itself as numerical instability.
In[11]:=
Click for copyable input
Out[11]=
This shows the values computed at the spatial grid points at the endpoint of the temporal integration.
In[12]:=
Click for copyable input
Out[14]=

It is easily seen from the point plot that the front has not been resolved.

This makes a 3D plot showing the time evolution for each of the spatial grid points. The initial condition is shown in red.
In[15]:=
Click for copyable input
Out[15]=

When a derivative of an InterpolatingFunction object is taken, a new InterpolatingFunction object is returned that gives the requested derivative when evaluated at a point. The is a way of determining what derivative will be evaluated.

The derivative returns a new InterpolatingFunction object.
In[16]:=
Click for copyable input
Out[16]=
This shows what derivative will be evaluated.
In[17]:=
Click for copyable input
Out[17]=

NDSolveUtilities

A number of utility routines have been written to facilitate the investigation and comparison of various NDSolve methods. These functions have been collected in the package .

CompareMethods[sys,refsol,methods,opts]return statistics for various methods applied to the system sys
FinalSolutions[sys,sols]return the solution values at the end of the numerical integration for various solutions sols corresponding to the system sys
InvariantErrorPlot[invts,dvars,ivar,sol,opts]return a plot of the error in the invariants invts for the solution sol
RungeKuttaLinearStabilityFunction[amat,bvec,var]return the linear stability function for the RungeKutta method with coefficient matrix amat and weight vector bvec using the variable var
StepDataPlot[sols,opts]return plots of the step sizes taken for the solutions sols on a logarithmic scale

Functions provided in the package.

This loads the package.
In[18]:=
Click for copyable input

A useful means of analyzing RungeKutta methods is to study how they behave when applied to a scalar linear test problem (see the package FunctionApproximations.m).

This assigns the (exact or infinitely precise) coefficients for the 2-stage implicit RungeKutta Gauss method of order 4.
In[19]:=
Click for copyable input
Out[19]=
This computes the linear stability function, which corresponds to the (2,2) Padé approximation to the exponential at the origin.
In[20]:=
Click for copyable input
Out[20]=

Examples of the functions , , , and can be found within "ExplicitRungeKutta Method for NDSolve".

Examples of the function can be found within "Projection Method for NDSolve".

InvariantErrorPlot Options

The function has a number of options that can be used to control the form of the result.

option name
default value
InvariantDimensionsAutomaticspecify the dimensions of the invariants
InvariantErrorFunctionAbs[Subtract[#1,#2]]&specify the function to use for comparing errors
InvariantErrorSampleRateAutomaticspecify how often errors are sampled

Options of the function .

The default value for is to determine the dimensions from the structure of the input, Dimensions[invts].

The default value for is a function to compute the absolute error.

The default value for is to sample all points if there are less than 1000 steps taken. Above this threshold a logarithmic sample rate is used.