Numerical Solution of Differential Equations

The function NDSolve discussed in "Numerical Differential Equations" allows you to find numerical solutions to differential equations. NDSolve handles both single differential equations and sets of simultaneous differential equations. It can handle a wide range of ordinary differential equations as well as some partial differential equations. In a system of ordinary differential equations there can be any number of unknown functions , but all of these functions must depend on a single "independent variable" , which is the same for each function. Partial differential equations involve two or more independent variables. NDSolve can also handle differentialalgebraic equations that mix differential equations with algebraic ones.

find a numerical solution for the function Null with Null in the range xmin to xmax
find numerical solutions for several functions Null

Finding numerical solutions to ordinary differential equations.

NDSolve represents solutions for the functions Null as InterpolatingFunction objects. The InterpolatingFunction objects provide approximations to the Null over the range of values Null to Null for the independent variable Null.

NDSolve finds solutions iteratively. It starts at a particular value of Null, then takes a sequence of steps, trying eventually to cover the whole range Null to Null.

In order to get started, NDSolve has to be given appropriate initial or boundary conditions for the Null and their derivatives. These conditions specify values for Null, and perhaps derivatives Null, at particular points Null. In general, at least for ordinary differential equations, the conditions you give can be at any Null: NDSolve will automatically cover the range Null to Null.

This finds a solution for y with x in the range 0 to 2, using an initial condition for y[0]:
Click for copyable input
This still finds a solution with x in the range 0 to 2, but now the initial condition is for y[3]:
Click for copyable input
Here is a simple boundary value problem:
Click for copyable input

When you use NDSolve, the initial or boundary conditions you give must be sufficient to determine the solutions for the Null completely. When you use DSolve to find symbolic solutions to differential equations, you can get away with specifying fewer initial conditions. The reason is that DSolve automatically inserts arbitrary constants C[i] to represent degrees of freedom associated with initial conditions that you have not specified explicitly. Since NDSolve must give a numerical solution, it cannot represent these kinds of additional degrees of freedom. As a result, you must explicitly give all the initial or boundary conditions that are needed to determine the solution.

In a typical case, if you have differential equations with up to derivatives, then you need to give initial conditions for up to derivatives, or give boundary conditions at points.

With a thirdorder equation, you need to give initial conditions for up to second derivatives:
Click for copyable input
This plots the solution obtained:
Click for copyable input
With a thirdorder equation, you can also give boundary conditions at three points:
Click for copyable input
The Wolfram Language allows you to use any appropriate linear combination of function values and derivatives as boundary conditions:
Click for copyable input

In most cases, all the initial conditions you give must involve the same value of Null, say Null. As a result, you can avoid giving both Null and Null explicitly. If you specify your range of Null as Null, then the Wolfram Language will automatically generate a solution over the range Null to Null.

This generates a solution over the range 0 to 2:
Click for copyable input

You can give initial conditions as equations of any kind. In some cases, these equations may have multiple solutions. In such cases, NDSolve will correspondingly generate multiple solutions.

The initial conditions in this case lead to multiple solutions:
Click for copyable input
Here is a plot of all the solutions:
Click for copyable input

You can use NDSolve to solve systems of coupled differential equations.

This finds a numerical solution to a pair of coupled equations:
Click for copyable input
This plots the solution for y from these equations:
Click for copyable input
This generates a parametric plot using both x and y:
Click for copyable input

Unknown functions in differential equations do not necessarily have to be represented by single symbols. If you have a large number of unknown functions, you will often find it more convenient, for example, to give the functions names like y[i].

This constructs a set of five coupled differential equations and initial conditions:
Click for copyable input
This solves the equations:
Click for copyable input
Here is a plot of the solutions:
Click for copyable input

NDSolve can handle functions whose values are lists or arrays. If you give initial conditions like y[0]=={v1,v2,,vn}, then NDSolve will assume that Null is a function whose values are lists of length Null.

This solves a system of four coupled differential equations:
Click for copyable input
Here are the solutions:
Click for copyable input
option name
default value
MaxStepsAutomaticmaximum number of steps in to take
StartingStepSizeAutomaticstarting size of step in to use
MaxStepSizeAutomaticmaximum size of step in to use
NormFunctionAutomaticthe norm to use for error estimation

Special options for NDSolve.

NDSolve has many methods for solving equations, but essentially all of them at some level work by taking a sequence of steps in the independent variable , and using an adaptive procedure to determine the size of these steps. In general, if the solution appears to be varying rapidly in a particular region, then NDSolve will reduce the step size or change the method so as to be able to track the solution better.

This solves a differential equation in which the derivative has a discontinuity:
Click for copyable input
NDSolve reduced the step size around so as to reproduce the kink accurately:
Click for copyable input

Through its adaptive procedure, NDSolve is able to solve "stiff" differential equations in which there are several components that vary with at very different rates.

In these equations, y varies much more rapidly than z:
Click for copyable input
NDSolve nevertheless tracks both components successfully:
Click for copyable input

NDSolve follows the general procedure of reducing step size until it tracks solutions accurately. There is a problem, however, when the true solution has a singularity or the integration interval is too big. For the first case, NDSolve limits the smallest step size that it will consider as significant for a given integration interval. For the second case, the option MaxSteps specifies the maximum number of steps that NDSolve will ever take in attempting to find a solution. For ordinary differential equations the default setting is MaxStepsAutomatic. With the Automatic setting, NDSolve will estimate how many steps are needed to solve the equation at hand based on the initial step sizes taken.

NDSolve stops when the step size becomes too small:
Click for copyable input
There is in fact a singularity in the solution at :
Click for copyable input

The default setting for MaxSteps should be sufficient for most equations with smooth solutions. When solutions have a complicated structure, however, you may occasionally have to choose larger settings for MaxSteps. With the setting MaxSteps->Infinity, there is no upper limit on the number of steps used.

To take the solution to the Lorenz equations this far, you need to remove the default bound on MaxSteps:
Click for copyable input
Here is a parametric plot of the solution in three dimensions:
Click for copyable input

When NDSolve solves a particular set of differential equations, it always tries to choose a step size appropriate for those equations. In some cases, the very first step that NDSolve makes may be too large, and it may miss an important feature in the solution. To avoid this problem, you can explicitly set the option StartingStepSize to specify the size to use for the first step.

The equations you give to NDSolve do not necessarily all have to involve derivatives; they can also just be algebraic. You can use NDSolve to solve many such differentialalgebraic equations.

This solves a system of differentialalgebraic equations:
Click for copyable input
Here is the solution:
Click for copyable input
solve a system of partial differential equations for Null
solve a system of partial differential equations for several functions Null

Finding numerical solutions to partial differential equations.

This finds a numerical solution to the wave equation. The result is a twodimensional interpolating function:
Click for copyable input
This generates a plot of the result:
Click for copyable input
This finds a numerical solution to a nonlinear wave equation:
Click for copyable input
Here is a 3D plot of the result:
Click for copyable input
This is a higherresolution density plot of the solution:
Click for copyable input
Here is a version of the equation in 2+1 dimensions:
Click for copyable input
This solves the equation:
Click for copyable input
This generates a list of plots of the solution:
Click for copyable input