Numerical Differential Equation Analysis Package
The
NumericalDifferentialEquationAnalysis package combines functionality for analyzing differential equations using Butcher trees, Gaussian quadrature, and NewtonCotes quadrature.
Butcher
RungeKutta methods are useful for numerically solving certain types of ordinary differential equations. Deriving highorder RungeKutta methods is no easy task, however. There are several reasons for this. The first difficulty is in finding the socalled order conditions. These are nonlinear equations in the coefficients for the method that must be satisfied to make the error in the method of order
O (h^{n}) for some integer
n where
h is the step size. The second difficulty is in solving these equations. Besides being nonlinear, there is generally no unique solution, and many heuristics and simplifying assumptions are usually made. Finally, there is the problem of combinatorial explosion. For a twelfthorder method there are 7813 order conditions!
This package performs the first task: finding the order conditions that must be satisfied. The result is expressed in terms of unknown coefficients
a_{ij},
b_{j}, and
c_{i}. The
sstage RungeKutta method to advance from
x to
x+h is then
Sums of the elements in the rows of the matrix
[a_{ij}] occur repeatedly in the conditions imposed on
a_{ij} and
b_{j}. In recognition of this and as a notational convenience it is usual to introduce the coefficients
c_{i} and the definition
This definition is referred to as the rowsum condition and is the first in a sequence of rowsimplifying conditions.
If
a_{ij}=0 for all
i<=j the method is
explicit; that is, each of the
Y_{i} (x+h) is defined in terms of previously computed values. If the matrix
[a_{ij}] is not strictly lower triangular, the method is
implicit and requires the solution of a (generally nonlinear) system of equations for each timestep. A diagonally implicit method has
a_{ij}=0 for all
i<j.
There are several ways to express the order conditions. If the number of stages
s is specified as a positive integer, the order conditions are expressed in terms of sums of explicit terms. If the number of stages is specified as a symbol, the order conditions will involve symbolic sums. If the number of stages is not specified at all, the order conditions will be expressed in stageindependent tensor notation. In addition to the matrix
a and the vectors
b and
c, this notation involves the vector
e, which is composed of all ones. This notation has two distinct advantages: it is independent of the number of stages
s and it is independent of the particular RungeKutta method.
For further details of the theory see the references.
a_{i,j}  the coefficient of f(Y_{j}(x)) in the formula for Y_{i}(x) of the method 
b_{j}  the coefficient of f(Y_{j}(x)) in the formula for Y(x) of the method 
c_{i}  a notational convenience for a_{ij} 
e  a notational convenience for the vector (1, 1, 1, ...) 
Notation used by functions for Butcher.
Functions associated with the order conditions of RungeKutta methods.
ButcherRowSum  specify whether the rowsum conditions for the c_{i} should be explicitly included in the list of order conditions 
ButcherSimplify  specify whether to apply Butcher's row and column simplifying assumptions 
Some options for RungeKuttaOrderConditions.
This gives the number of order conditions for each order up through order 10. Notice the combinatorial explosion.
Out[2]=  

This gives the order conditions that must be satisfied by any firstorder, 3stage RungeKutta method, explicitly including the rowsum conditions.
Out[3]=  

These are the order conditions that must be satisfied by any secondorder, 3stage RungeKutta method. Here the rowsum conditions are not included.
Out[4]=  

It should be noted that the sums involved on the lefthand sides of the order conditions will be left in symbolic form and not expanded if the number of stages is left as a symbolic argument. This will greatly simplify the results for highorder, manystage methods. An even more compact form results if you do not specify the number of stages at all and the answer is given in tensor form.
These are the order conditions that must be satisfied by any secondorder, sstage method.
Out[5]=  

These are the order conditions that must be satisfied by any secondorder method. This uses tensor notation. The vector e is a vector of ones whose length is the number of stages.
Out[7]=  

The tensor notation can likewise be expanded to give the conditions in full.
Out[8]=  

These are the principal error coefficients for any thirdorder method.
Out[9]=  

This is a bound on the local error of any thirdorder method in the limit as h approaches 0, normalized to eliminate the effects of the ODE.
Out[10]=  

Here are the order conditions that must be satisfied by any fourthorder, 1stage RungeKutta method. Note that there is no possible way for these order conditions to be satisfied; there need to be more stages (the second argument must be larger) for there to be sufficiently many unknowns to satisfy all of the conditions.
Out[11]=  

Controlling the type of RungeKutta method in RungeKuttaOrderConditions and related functions.
RungeKuttaOrderConditions and certain related functions have the option
RungeKuttaMethod with default setting
$RungeKuttaMethod. Normally you will want to determine the RungeKutta method being considered by setting
$RungeKuttaMethod to one of
Implicit,
DiagonallyImplicit, and
Explicit, but you can specify an option setting or even change the default for an individual function.
These are the order conditions that must be satisfied by any secondorder, 3stage diagonally implicit RungeKutta method.
Out[12]=  

An alternative (but less efficient) way to get a diagonally implicit method is to force a to be lower triangular by replacing uppertriangular elements with 0.
Out[13]=  

These are the order conditions that must be satisfied by any thirdorder, 2stage explicit RungeKutta method. The contradiction in the order conditions indicates that no such method is possible, a result which holds for any explicit RungeKutta method when the number of stages is less than the order.
Out[14]=  

More functions associated with the order conditions of RungeKutta methods.
Butcher showed that the number and complexity of the order conditions can be reduced considerably at high orders by the adoption of socalled simplifying assumptions. For example, this reduction can be accomplished by adopting sufficient row and column simplifying assumptions and quadraturetype order conditions. The option
ButcherSimplify in
RungeKuttaOrderConditions can be used to determine these automatically.
These are the column simplifying conditions up to order 4.
Out[15]=  

These are the row simplifying conditions up to order 4.
Out[16]=  

These are the quadrature conditions up to order 4.
Out[17]=  

Trees are fundamental objects in Butcher's formalism. They yield both the derivative in a power series expansion of a RungeKutta method
and the related order constraint on the coefficients. This package provides a number of functions related to Butcher trees.
f  the elementary symbol used in the representation of Butcher trees 
ButcherTrees[p]  give a list, partitioned by order, of the trees for any RungeKutta method of order p 
ButcherTreeSimplify[p,,]  give the set of trees through order p that are not reduced by Butcher's simplifying assumptions, assuming that the quadrature conditions through order p, the row simplifying conditions through order , and the column simplifying conditions through order all hold. The result is grouped by order, starting with the first nonvanishing trees 
ButcherTreeCount[p]  give a list of the number of trees through order p 
ButcherTreeQ[tree]  give True if the tree or list of trees tree is valid functional syntax, and False otherwise 
Constructing and enumerating Butcher trees.
This gives the trees that are needed for any thirdorder method. The trees are represented in a functional form in terms of the elementary symbol f.
Out[18]=  

This tests the validity of the syntax of two trees. Butcher trees must be constructed using multiplication, exponentiation or application of the function f.
Out[19]=  

This evaluates the number of trees at each order through order 10. The result is equivalent to Out[2] but the calculation is much more efficient since it does not actually involve constructing order conditions or trees.
Out[20]=  

The previous result can be used to calculate the total number of trees required at each order through order 10.
Out[21]=  

The number of constraints for a method using row and column simplifying assumptions depends upon the number of stages.
ButcherTreeSimplify gives the Butcher trees that are not reduced assuming that these assumptions hold.
This gives the additional trees that are necessary for a fourthorder method assuming that the quadrature conditions through order 4 and the row and column simplifying assumptions of order 1 hold. The result is a single tree of order 4 (which corresponds to a single fourthorder condition).
Out[22]=  

It is often useful to be able to visualize a tree or forest of trees graphically. For example, depicting trees yields insight, which can in turn be used to aid in the construction of RungeKutta methods.
ButcherPlot[tree]  give a plot of the tree tree 
ButcherPlot[{tree_{1},tree_{2},...}]  give an array of plots of the trees in the forest {tree_{1}, tree_{2},...} 
Drawing Butcher trees.
Options to ButcherPlot.
This plots and labels the trees through order 4.
Out[23]=  

In addition to generating and drawing Butcher trees, many functions are provided for measuring and manipulating them. For a complete description of the importance of these functions, see Butcher
[1].
ButcherHeight[tree]  give the height of the tree tree 
ButcherWidth[tree]  give the width of the tree tree 
ButcherOrder[tree]  give the order, or number of vertices, of the tree tree 
ButcherAlpha[tree]  give the number of ways of labeling the vertices of the tree tree with a totally ordered set of labels such that if (m, n) is an edge, then m<n 
ButcherBeta[tree]  give the number of ways of labeling the tree tree with ButcherOrder[tree]1 distinct labels such that the root is not labeled, but every other vertex is labeled 
ButcherBeta[n,tree]  give the number of ways of labeling n of the vertices of the tree with n distinct labels such that every leaf is labeled and the root is not labeled 
ButcherBetaBar[tree]  give the number of ways of labeling the tree tree with ButcherOrder[tree] distinct labels such that every node, including the root, is labeled 
ButcherBetaBar[n,tree]  give the number of ways of labeling n of the vertices of the tree with n distinct labels such that every leaf is labeled 
ButcherGamma[tree]  give the density of the tree tree; the reciprocal of the density is the righthand side of the order condition imposed by tree 
ButcherPhi[tree,s]  give the weight of the tree tree; the weight (tree) is the lefthand side of the order condition imposed by tree 
ButcherPhi[tree]  give (tree) using tensor notation 
ButcherSigma[tree]  give the order of the symmetry group of isomorphisms of the tree tree with itself 
Other functions associated with Butcher trees.
This gives the order of the tree f[f[f[f] f^2]].
Out[24]=  

This gives the density of the tree f[f[f[f] f^2]].
Out[25]=  

This gives the elementary weight function imposed by f[f[f[f] f^2]] for an sstage method.
Out[26]=  

The subscript notation is a formatting device and the subscripts are really just the indexed variable NumericalDifferentialEquationAnalysis`Private`$i.
Out[27]//FullForm= 
 

It is also possible to obtain solutions to the order conditions using
Solve and related functions. Many issues related to the construction RungeKutta methods using this package can be found in Sofroniou
[6]. The article also contains details concerning algorithms used in
Butcher.m and discusses applications.
Gaussian Quadrature
As one of its methods, the
Mathematica function
NIntegrate uses a fairly sophisticated GaussKronrodbased algorithm. The Gaussian quadrature functionality provided in
NumericalDifferentialEquationAnalysis allows you to easily study some of the theory behind ordinary Gaussian quadrature which is a little less sophisticated.
The basic idea behind Gaussian quadrature is to approximate the value if an integral as a linear combination of values of the integrand evaluated at specific points:
Since there are
2n free parameters to be chosen (both the abscissas
x_{i} and the weights
w_{i}) and since both integration and the sum are linear operations, you can expect to be able to make the formula correct for all polynomials of degree less than about
2n. In addition to knowing what the optimal abscissas and weights are, it is often desirable to know how large the error in the approximation will be. This package allows you to answer both of these questions.
GaussianQuadratureWeights[n,a,b]  give a list of the pairs (x_{i}, w_{i}) to machine precision for quadrature on the interval a to b 

 give the error to machine precision 

 give a list of the pairs (x_{i}, w_{i}) to precision prec 

 give the error to precision prec 
Finding formulas for Gaussian quadrature.
This gives the abscissas and weights for the fivepoint Gaussian quadrature formula on the interval ( 3, 7).
Out[2]=  

Here is the error in that formula. Unfortunately it involves the tenth derivative of f at an unknown point so you don't really know what the error itself is.
Out[3]=  

You can see that the error decreases rapidly with the length of the interval.
Out[4]=  

NewtonCotes
As one of its methods, the
Mathematica function
NIntegrate uses a fairly sophisticated GaussKronrod based algorithm. Other types of quadrature formulas exist, each with their own advantages. For example, Gaussian quadrature uses values of the integrand at oddly spaced abscissas. If you want to integrate a function presented in tabular form at equally spaced abscissas, it won't work very well. An alternative is to use NewtonCotes quadrature.
The basic idea behind NewtonCotes quadrature is to approximate the value of an integral as a linear combination of values of the integrand evaluated at equally spaced points:
In addition, there is the question of whether or not to include the end points in the sum. If they are included, the quadrature formula is referred to as a closed formula. If not, it is an open formula. If the formula is open there is some ambiguity as to where the first abscissa is to be placed. The open formulas given in this package have the first abscissa one half step from the lower end point.
Since there are
n free parameters to be chosen (the weights) and since both integration and the sum are linear operations, you can expect to be able to make the formula correct for all polynomials of degree less than about
n. In addition to knowing what the weights are, it is often desirable to know how large the error in the approximation will be. This package allows you to answer both of these questions.
Finding formulas for NewtonCotes quadrature.
Option for NewtonCotesWeights and NewtonCotesError.
Here are the abscissas and weights for the fivepoint closed NewtonCotes quadrature formula on the interval (3, 7).
Out[2]=  

Here is the error in that formula. Unfortunately it involves the sixth derivative of f at an unknown point so you don't really know what the error itself is.
Out[3]=  

You can see that the error decreases rapidly with the length of the interval.
Out[4]=  

This gives the abscissas and weights for the fivepoint open NewtonCotes quadrature formula on the interval ( 3, 7).
Out[5]=  

Here is the error in that formula.
Out[6]=  
