Differential Equations

You can use the Wolfram Language function (RefLink[DSolve, paclet:ref/DSolve]) to find symbolic solutions to ordinary and partial differential equations.

Solving a differential equation consists essentially in finding the form of an unknown function. In the Wolfram Language, unknown functions are represented by expressions like y[x]. The derivatives of such functions are represented by y'[x], y''[x], and so on.

The Wolfram Language function (RefLink[DSolve, paclet:ref/DSolve]) returns as its result a list of rules for functions. There is a question of how these functions are represented. If you ask (RefLink[DSolve, paclet:ref/DSolve]) to solve for y[x], then (RefLink[DSolve, paclet:ref/DSolve]) will indeed return a rule for y[x]. In some cases, this rule may be all you need. But this rule, on its own, does not give values for y'[x] or even y[0]. In many cases, therefore, it is better to ask (RefLink[DSolve, paclet:ref/DSolve]) to solve not for y[x], but instead for y itself. In this case, what (RefLink[DSolve, paclet:ref/DSolve]) will return is a rule which gives y as a pure function, in the sense discussed in "Pure Functions".

If you ask (RefLink[DSolve, paclet:ref/DSolve]) to solve for y[x], it will give a rule specifically for y[x]:

DSolve[y'[x]+y[x]==1,y[x],x]

The rule applies only to y[x] itself, and not, for example, to objects like y[0] or y'[x]:

y[x]+2y'[x]+y[0]/.%

If you ask (RefLink[DSolve, paclet:ref/DSolve]) to solve for y, it gives a rule for the object y on its own as a pure function:

DSolve[y'[x]+y[x]==1,y,x]

Now the rule applies to all occurrences of y:

y[x]+2y'[x]+y[0]/.%

Substituting the solution into the original equation yields (RefLink[True, paclet:ref/True]):

y'[x]+y[x]==1/.%%

Getting solutions to differential equations in different forms.

In standard mathematical notation, one typically represents solutions to differential equations by explicitly introducing "dummy variables" to represent the arguments of the functions that appear. If all you need is a symbolic form for the solution, then introducing such dummy variables may be convenient. However, if you actually intend to use the solution in a variety of other computations, then you will usually find it better to get the solution in pure-function form, without dummy variables. Notice that this form, while easy to represent in the Wolfram Language, has no direct analog in standard mathematical notation.

Solving simultaneous differential equations.

This solves two simultaneous differential equations:

DSolve[{y[x]==-z'[x],z[x]==-y'[x]},{y,z},x]

The Wolfram Language returns two distinct solutions for y in this case:

DSolve[y[x] y'[x]==1,y,x]

You can add constraints and boundary conditions for differential equations by explicitly giving additional equations such as y[0]==0.

This asks for a solution which satisfies the condition y[0]==1:

DSolve[{y'[x]==a y[x],y[0]==1},y[x],x]

If you ask the Wolfram Language to solve a set of differential equations and you do not give any constraints or boundary conditions, then the Wolfram Language will try to find a general solution to your equations. This general solution will involve various undetermined constants. One new constant is introduced for each order of derivative in each equation you give.

The default is that these constants are named (RefLink[C, paclet:ref/C])[n], where the index n starts at 1 for each invocation of (RefLink[DSolve, paclet:ref/DSolve]). You can override this choice, by explicitly giving a setting for the option (RefLink[GeneratedParameters, paclet:ref/GeneratedParameters]). Any function you give is applied to each successive index value n to get the constants to use for each invocation of (RefLink[DSolve, paclet:ref/DSolve]).

The general solution to this fourth-order equation involves four undetermined constants:

DSolve[y''''[x]==y[x],y[x],x]

Each independent initial or boundary condition you give reduces the number of undetermined constants by one:

DSolve[{y''''[x]==y[x],y[0]==y'[0]==0},y[x],x]

You should realize that finding exact formulas for the solutions to differential equations is a difficult matter. In fact, there are only fairly few kinds of equations for which such formulas can be found, at least in terms of standard mathematical functions.

The most widely investigated differential equations are linear ones, in which the functions you are solving for, as well as their derivatives, appear only linearly.

This is a homogeneous first-order linear differential equation, and its solution is quite simple:

DSolve[y'[x]-x y[x]==0,y[x],x]

Making the equation inhomogeneous leads to a significantly more complicated solution:

DSolve[y'[x]-x y[x]==1,y[x],x]

If you have only a single linear differential equation, and it involves only a first derivative of the function you are solving for, then it turns out that the solution can always be found just by doing integrals.

But as soon as you have more than one differential equation, or more than a first-order derivative, this is no longer true. However, some simple second-order linear differential equations can nevertheless be solved using various special functions from "Special Functions". Indeed, historically many of these special functions were first introduced specifically in order to represent the solutions to such equations.

This is Airy’s equation, which is solved in terms of Airy functions:

DSolve[y''[x]-x y[x]==0,y[x],x]

This equation comes out in terms of Bessel functions:

DSolve[y''[x]-Exp[x] y[x]==0,y[x],x]

This requires Mathieu functions:

DSolve[y''[x]+Cos[x] y[x]==0,y,x]

And this Legendre functions:

DSolve[y''[x]-Cot[x]^2 y[x]==0,y[x],x]

Occasionally second-order linear equations can be solved using only elementary functions:

DSolve[x^2 y''[x]+y[x]==0,y[x],x]

Beyond second order, the kinds of functions needed to solve even fairly simple linear differential equations become extremely complicated. At third order, the generalized Meijer G function (RefLink[MeijerG, paclet:ref/MeijerG]) can sometimes be used, but at fourth order and beyond absolutely no standard mathematical functions are typically adequate, except in very special cases.

Here is a third-order linear differential equation which can be solved in terms of generalized hypergeometric functions:

DSolve[y'''[x]+x y[x]==0,y[x],x]

This requires more general Meijer G functions:

DSolve[y'''[x]+Exp[x] y[x]==0,y[x],x]

For nonlinear differential equations, only rather special cases can usually ever be solved in terms of standard mathematical functions. Nevertheless, (RefLink[DSolve, paclet:ref/DSolve]) includes fairly general procedures which allow it to handle almost all nonlinear differential equations whose solutions are found in standard reference books.

First-order nonlinear differential equations in which x does not appear on its own are fairly easy to solve:

DSolve[y'[x]-y[x]^2==0,y[x],x]

This Riccati equation already gives a significantly more complicated solution:

DSolve[y'[x]-y[x]^2==x,y[x],x]//FullSimplify

This Bernoulli equation, however, has a fairly simple solution:

DSolve[y'[x]-x y[x]^2-y[x]==0,y[x],x]

An n(^th) order Bernoulli equation typically has n-1 distinct solutions:

DSolve[y'[x]-x y[x]^3+y[x]==0,y[x],x]

This Abel equation can be solved, but only implicitly:

DSolve[y'[x]+x y[x]^3+y[x]^2==0,y[x],x]

In practical applications, it is quite often convenient to set up differential equations that involve piecewise functions. You can use (RefLink[DSolve, paclet:ref/DSolve]) to find symbolic solutions to such equations.

This equation involves a piecewise forcing function:

DSolve[y'[x]-y[x]==UnitStep[x],y[x],x]

Here the solution is explicitly broken into three cases:

DSolve[y'[x]+Clip[x] y[x]==0,y[x],x]

Beyond ordinary differential equations, one can consider differential-algebraic equations that involve a mixture of differential and algebraic equations.

This solves a differential-algebraic equation:

DSolve[{y'[x]+3z'[x]==4 y[x]+1/x,y[x]+z[x]==1},{y[x],z[x]},x]

Solving partial differential equations.

(RefLink[DSolve, paclet:ref/DSolve]) is set up to handle not only ordinary differential equations in which just a single independent variable appears, but also partial differential equations in which two or more independent variables appear.

This finds the general solution to a simple partial differential equation with two independent variables:

DSolve[D[y[x1,x2],x1]+D[y[x1,x2],x2]==1/(x1 x2),y[x1,x2],{x1,x2}]

Here is the result represented as a pure function:

DSolve[D[y[x1,x2],x1]+D[y[x1,x2],x2]==1/(x1 x2),y,{x1,x2}]

The basic mathematics of partial differential equations is considerably more complicated than that of ordinary differential equations. One feature is that whereas the general solution to an ordinary differential equation involves only arbitrary constants, the general solution to a partial differential equation, if it can be found at all, must involve arbitrary functions. Indeed, with m independent variables, arbitrary functions of m-1 arguments appear. (RefLink[DSolve, paclet:ref/DSolve]) by default names these functions (RefLink[C, paclet:ref/C])[n].

Here is a simple PDE involving three independent variables:

(D[#,x1]+D[#,x2]+D[#,x3])&[y[x1,x2,x3]]==0

The solution involves an arbitrary function of two variables:

DSolve[%,y[x1,x2,x3],{x1,x2,x3}]

Here is the one-dimensional wave equation:

(c^2 D[#,x,x]-D[#,t,t])&[y[x,t]]==0

The solution to this second-order equation involves two arbitrary functions:

DSolve[%,y[x,t],{x,t}]

For an ordinary differential equation, it is guaranteed that a general solution must exist, with the property that adding initial or boundary conditions simply corresponds to forcing specific choices for arbitrary constants in the solution. But for partial differential equations this is no longer true. Indeed, it is only for linear partial differential and a few other special types that such general solutions exist.

Other partial differential equations can be solved only when specific initial or boundary values are given, and in the vast majority of cases no solutions can be found as exact formulas in terms of standard mathematical functions.

Since y and its derivatives appear only linearly here, a general solution exists:

DSolve[x1 D[y[x1,x2],x1]+x2 D[y[x1,x2],x2]==Exp[x1 x2],y[x1,x2],{x1,x2}]

This weakly nonlinear PDE turns out to have a general solution:

DSolve[D[y[x1,x2],x1]+D[y[x1,x2],x2]==Exp[y[x1,x2]],y[x1,x2],{x1,x2}]//Quiet

Here is a nonlinear PDE which has no general solution:

DSolve[D[y[x1,x2],x1] D[y[x1,x2],x2]==a,y[x1,x2],{x1,x2}]