Mathematica 9 is now available

How Do I Solve a Differential Equation in Mathematica?

Note: This is not a primer on differential calculus, differential equation solving methods, or numerical accuracy. If you are interested in any of these topics, please consult the appropriate literature. Some good books on these subjects can be found in the Mathematica bookstore.

One important advantage of Mathematica is the ease with which users can solve many families of differential equations either numerically or symbolically.

In many cases, it is a good idea to try the symbolic solver DSolve first. With the constant improvements made to Mathematica functions, DSolve can often find a complete analytic solution even for higher-order or nonlinear differential equations.

Symbolic Solutions

What Kinds of Problems Can DSolve Solve?

DSolve can solve linear ordinary differential equations of any order with constant coefficients. It can also solve many linear equations up to second order with nonconstant coefficients. DSolve includes general procedures for many of the nonlinear ordinary differential equations whose solutions are given in standard reference books such as Erich Kamke's Differentialgleichungen. Lösungsmethoden und Lösungen I: Gewöhnliche Differentialgleichungen. DSolve can also can find general solutions for linear and weakly nonlinear partial differential equations. Truly nonlinear partial differential equations usually admit no general solutions.

Ordinary Differential Equations

All most users will have to do is just type in the differential equation or the system of differential equations. As opposed to many numerical differential equation solvers, Mathematica does not require users to transform higher-order equations to systems of first-order equations by hand. Below are some examples. For more information, see the documentation on DSolve, especially the section "Further Examples."

Ordinary Differential Equations without Initial Conditions

In this case, you can usually just type in the equation and evaluate the DSolve statement.


DSolve also works for higher-order equations.


DSolve works as well for first-order equations with nonlinear coefficients.


Just for the fun of it, let's give Mathematica this differential equation:


Ordinary Differential Equations with Initial Conditions

These equations are handled pretty much the same as ordinary differential equations without initial conditions. Just enter the initial conditions as part of the list of equations.

DSolve[{Derivative[1][y][x] - x*y[x] == 0, y[0] == 0.1}, 
  y[x], x]
{{y[x] -> 0.1*E^(0.5*x^2)}}

Systems of Ordinary Differential Equations

Solving systems of ordinary differential equations is the same as solving a single ODE. Simply enter the list of equations and run DSolve on it.


Partial Differential Equations

The theory of partial differential equations is less fully developed than the theory of ordinary differential equations; specifically, there is no equivalent of the Cauchy-Kovalevskaya Theorem. This means that Mathematica can solve only a subset of differential equations symbolically. Specifically, DSolve can find general solutions for linear and weakly nonlinear partial differential equations. Most nonlinear partial differential equations do not allow for general solutions. In these cases it is advisable to use the tools in the Calculus`DSolveIntegrals` standard package to attempt to find complete integrals or differential invariants.

DSolve can find general solutions to partial differential equations like the wave equation in the following example. Once again, just enter the equation and call DSolve.

Return to Top

Numerical Solutions

In Mathematica, the NDSolve command works consistently for first-order ordinary differential equations, partial differential equations, and systems of differential equations and higher-order differential equations. Additionally, its default method, Automatic, automatically switches between Gear and Adams, depending on stiffness. Another possibility for equations that are not stiff is RungeKutta. For some problems, the Runge-Kutta method can find the solution faster than the other methods.

Higher-Order Differential Equations

NDSolve requires the differential equation(s), a sufficient set of initial conditions, and the range to solve for as inputs. The /. shortcut in the Plot command inserts the solution for the function [Graphics:Images/index_gr_21.gif].



Boundary-Value Problems

NDSolve can solve a significant number of boundary-value problems. Just add the boundary conditions as additional equations.



Systems of Differential Equations

In this example, Mathematica solves the equations of a Lorenz attractor in one step. In other packages, the user would have to do significant setup work.


Here is a graph of the solution.




Partial Differential Equations

In this example, NDSolve solves the heat equation with the left end [Graphics:Images/index_gr_30.gif] held at fixed temperature [Graphics:Images/index_gr_31.gif], the right end ([Graphics:Images/index_gr_32.gif]) held at fixed temperature [Graphics:Images/index_gr_33.gif], and an initial heat profile given by a quadratic in x.



A more interesting example is when time-dependent boundary conditions are added. For example, entering the following produces a plot of a solution with the temperature at the left edge varying sinusoidally.



Systems of Partial Differential Equations

NDSolve can also handle systems of partial differential equations. The notation [Graphics:Images/index_gr_38.gif] means "replace the functions [Graphics:Images/index_gr_39.gif] and [Graphics:Images/index_gr_40.gif] with the results of NDSolve."



Return to Top

Checking Results

The easiest way to check results in Mathematica is to substitute the solution into the original equation. This technique works for both symbolic and numerical solutions.


/.symsol replaces [Graphics:Images/index_gr_49.gif] with the solution found by DSolve.


The solution satisfies the differential equation.



The solution found by NDSolve satisfies the equation in very good approximation. The rather high difference at the beginning is an artifact of the numerical differentiation. See the next section for how to arrive at even more-accurate solutions.

Return to Top

Higher-Precision Settings

One way to get a very precise solution of an ODE is to give a sufficiently high value for the WorkingPrecision option. Note that AccuracyGoal and PrecisionGoal default to 10 less than the value of WorkingPrecision when it is greater than $MachinePrecision.

Let's compare the result of an earlier calculation to the same one running with higher precision.


The following plot shows the logarithm of the difference between the numerical solution and the differential equation with the default setting of WorkingPrecision (black) and with WorkingPrecision set to 22 (red).



As you can see, the numerical errors are significantly smaller when the higher working precision is used. The rather high first value is an artifact.

Return to Top

Other Places to Look


Most nonlinear partial differential equations do not allow for general solutions. In these cases it is advisable to use the tools in the Calculus`DSolveIntegrals` standard add-on package to attempt to find complete integrals or differential invariants.

Integral Transforms

Mathematica includes many functions for calculating integral transforms. Fourier and Laplace transforms are especially powerful tools for finding analytical solutions to differential equations. See the sections about Fourier and Laplace transforms in The Mathematica Book for more information.

Lie Symmetries

Gerd Baumann of the University of Ulm in Germany has submitted a number of packages that deal with Lie and Lie-Baecklund symmetries. Lie symmetry methods can be used to construct solutions for linear and especially for nonlinear systems of differential equations.

Return to Top