# Differential Equations

A (first order) differential equation of the form*y' = f(x,y)*expresses rate of change of the dependent variable

*y*with respect to a change of the independent variable

*x*as a function

*f(x,y)*of both the independent variable

*x*and the dependent variable

*y*. A solution of the differential equation

*y' = f(x,y)*is a function, which by abuse of language we denote as

*y(x)*, such that

*y'(x) = f(x,y(x))*. The existence and uniqueness theorem of first order ordinary differential equations states that if the function

*f*is continuous and satisfies the Lipschitz condition on some closed region

*U×V*, with

*x*in

*U*and

*y*in

*V*, containing the interior point

*(x*, then there exists a unique solution

_{0}, y_{0})*y(x)*and a subinterval

*U*containing

_{0}*x*and contained in

_{0}*U*such that

*y(x*and

_{0}) = y_{0}*(x,y(x))*is contained in

*U×V*for

*x*in

*U*. The condition that

_{0}*y(x*is called the initial condition. If the function

_{0}) = y_{0}*f*is differentiable in the closed region, then the Lipschitz condition is automatically satisfied. Furthermore, if the function

*f*is

*C*, i.e.

^{ n}*f*has continuous derivatives up to order

*n*, for

*n > 0*, in the closed region, then the solution is

*C*in a neighborhood of

^{ n+1}*x*. Note that if the Lipschitz condition is not satisfied, then either there does not exist a solution or there may exist more than one solution.

_{0}If the function

*f(x,y)*is independent of the dependent variable

*y*, then the solution is

*y(x) = y*, where the integral can be approximated by one of the numerical techniques in the section Numerical Integration.

_{0}+ ∫^{x}_{x0}f(t) dtIf the function

*f(x,y)*is independent of the independent variable

*x*, the differential equation

*y'=f(y)*is said to be an autonomous differential equation. The methods given below apply to both autonomous and non-autonomous differential equations but in the autonomous case, the user may want to edit the files and remove references to the independent variable wherever the independent variable is referenced through

*f*.

**First Order System of Differential Equations - Higher Order Differential Equation**

The dependent variable

*y*can be a vector quantity

*in which case we have a system of first order differential equations. The existence and uniqueness theorem stated above immediately generalizes to include the situation in which*

**y***is an*

**y***n*-dimensional vector.

An

*n*order differential equation

^{th}*y*can be transformed to a system of first order differential equations by defining the vector quantity

^{(n)}= f(x,y,y',y^{(2)},...,y^{(n-1)})*by*

**z**=(z_{1},...,z_{n})*z*,

_{1}=y*z*,

_{2}=y'*z*,...,

_{3}=y^{(2)}*z*, then

_{n}=y^{(n-1)}*z'*,

_{1}=z_{2}*z'*,...,

_{2}=z_{3}*z'*, and

_{n-1}=z_{n}*z'*.

_{n}=f(x,z_{1},...,z_{n})While a second order differential equation can be transfomed to a first order system as described above but because second order differential equations are ubiquitous in physics and engineering special methods have been developed for solving them, see Methods for Second-Order Differential Equations.

**Numerical Approximation of the Solutions of a Differential Equation**

For certain classes of differential equations, a solution can be found by finding an integrating factor and solving the differential equation exactly or expanding the solution in terms of a Taylor series and summing or (rarely) using Picard's theorem, or expanding the solution in terms of a class of orthogonal functions. Another approach for which the following source code is developed is called the difference method. For the difference method, the solution of the differential equation is approximated at discrete points, usually equally-spaced but not necessarily so. The most commonly used difference methods are Euler's Method,Trapezoidal Method, Midpoint Method, Modified Midpoint Method (Gragg's Method), Runge-Kutta Methods, Predictor-Corrector Methods, and certain adaptive techniques such as the embedded Runge-Kutta methods and the Gragg-Bulirsch-Stoer method.

The question of which method to use to solve a differential equation depends on the application. If one wants to write a general differential equation solver, then one should consider the adaptive methods: the embedded Runge-Kutta methods and the Gragg-Bulirsch-Stoer method. It is possible to adapt the predictor-corrector methods to adaptive techniques. If one wants to solve a particular parametric family of differential equations, and the application is a real-time application in which the time spent solving the equation must be known, then the implicit methods have to be avoided and only the explicit methods considered: Euler's method, the midpoint method, the modified midpoint method, and the Runge-Kutta methods. The questions concerning the step size(s) and order, in the case of Runge-Kutta methods, need to be determined apriori by solving the parametric differential equations for different values of the parameters and choosing the largest step size(s) and smallest order for which the numerical solution is sufficiently accurate. If the application is not real-time then the implicit methods do not have to be avoided and any method can be considered.

## Euler's Method

Given an initial value problem*y ' = f(x,y); y(x*and a step size

_{0}) = y_{0}*h*, Euler's method approximates the derivative of the solution of the initial value problem at

*x*by

*y '(x) = ( y(x+h) - y(x) ) / h*. The approximation

*y*for

_{n}*y(x*is then given recursively by

_{0}+nh)*y*

_{n+1}= y_{n}+ h f(x_{n},y_{n})*n = 0, 1, ...*.

Locally Euler's method is a second order method and therefore globally a first order method. As a rule, Euler's method is only useful for a few steps and small step sizes, however Euler's method together with Richardson extrapolation may be used to increase the order.

## Trapezoidal Method

The trapezoidal method is an implicit method which is locally a third order method and therefore globally a second order method.## Midpoint Method

The midpoint method also called the leapfrog method is a two-step method which is also locally a third order method or globally a second order method. Being a two-step method, the midpoint method requires two starting values to start the recursion. Usually this means that another method must be used to create the second starting value. Small perturbations of the initial conditions lead to growing oscillations of the errors, for this reason the use of the midpoint method should be avoided.## Modified Midpoint Method (Gragg's Method)

Gragg observed that a slight modification to the midpoint method would obviate the term giving rise to the growing error oscillations.## Runge-Kutta Methods

Runge-Kutta methods are one-step methods which evaluate the integrand several times at judicious points in a neighborhood of the initial point*(x*and then combines the results to form an approximation to

_{0}, y_{0})*y*at

*x*.

_{0}+ h## Adams-Bashforth Predictor Adams-Moulton Corrector

The Adams-Bashforth method and the Adams-Moulton method are multistep methods. A*k*-step method is one in which the approximation to the next value uses not only the current value of the dependent variable but also the previous

*k - 1*values.

The

*k*-step Adams-Bashforth method is the explicit method given by

*y*

_{n + 1}= y_{n}+ h * ( a_{0}f(x_{n},y_{n}) + a_{1}f(x_{n - 1},y_{n - 1}) + . . . + a_{k - 1}f(x_{n - k + 1},y_{n - k + 1}) )*a*are constants.

_{0}, a_{1}, . . . , a_{k - 1}The

*k*-step Adams-Moulton method is the implicit method given by

*y*

_{n + 1}= y_{n}+ h * ( b_{0}f(x_{n + 1},y_{n + 1}) + b_{1}f(x_{n},y_{n}) + . . . + b_{k}f(x_{n - k + 1},y_{n - k + 1}) )*b*are constants. The Adams-Bashforth prediction can be used as an initial estimate to the implicit Adams_Moulton method. The result is then iterated by replacing the old estimate of

_{0}, b_{1}, . . . , b_{k}*y*with the new estimate until the change in the estimates is sufficiently small. If the step size is sufficiently small, this iterative approach is contractive and convergence is quaranteed.

_{n + 1}## Embedded Runge-Kutta Methods

Embedded Runge-Kutta methods are used to estimate the not only the solution of an initial value problem but also the error. These methods are used in adaptive methods which control the step size so that the final answer will be within a preassigned tolerance.## Gragg-Bulirsch-Stoer Method

The Bulirsch-Stoer method is an adaptive method which uses Gragg's modified midpoint method to estimate the solution of an initial value problem for various step sizes. The estimates are fit a "diagonal" rational function as a function of the step size and the limit as the step size tends to zero is taken as the final estimate. This technique can be extremely useful especially in the neighborhood of poles or other singularities.## Methods for Second-Order Differential Equations

Second order differential equations frequently arise in Physics and Engineering problems. The fundamental equations of electrodynamics, quantum mechanics, and even classical mechanics are cast as partial differential equations which when solved frequently lead to second order ordinary differential equations. Although it is possible to transform any*n*order ordinary differential equation to a system of

^{ th}*n*first order equations, numerical methods have been developed to deal with second order ordinary differential equations directly.