Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

# dsolve

Differential equations and systems solver

Character vector inputs will be removed in a future release. Instead, use `syms` to declare variables and replace inputs such as `dsolve('Dy = y')` with ```syms y(t); dsolve(diff(y,t) == y)```.

## Syntax

``S = dsolve(eqn)``
``S = dsolve(eqn,cond)``
``S = dsolve(eqn,cond,Name,Value)``
``[y1,...,yN] = dsolve(___)``

## Description

example

````S = dsolve(eqn)` solves the differential equation `eqn`, where `eqn` is a symbolic equation. Use `diff` and `==` to represent differential equations. For example, ```diff(y,x) == y``` represents the equation `dy/dx=y`. Solve a system of differential equations by specifying `eqn` as a vector of those equations.```

example

````S = dsolve(eqn,cond)` solves `eqn` with the initial or boundary condition `cond`.```

example

````S = dsolve(eqn,cond,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.```

example

````[y1,...,yN] = dsolve(___)` assigns the solutions to the variables `y1,...,yN`.```

## Examples

### Solve Differential Equation

Specify a differential equation by using `==` and represent differentiation by using the `diff` function. Then, solve the equation by using `dsolve`.

Solve the equation $\frac{dy}{dt}=ay$.

```syms a y(t) eqn = diff(y,t) == a*y; dsolve(eqn)```
```ans = C2*exp(a*t)```

`C2` is a constant. To eliminate constants, see Solve Differential Equation with Condition. For a full workflow, see Solving Partial Differential Equations. For more examples, see Solve Differential Equation.

### Solve Higher-Order Differential Equation

Specify the second-order derivative of a function `y` by using `diff(y,t,2)` or `diff(y,t,t)`. Similarly, specify the n-th order derivative by using `diff(y,t,n)`.

Solve the equation $\frac{{d}^{2}y}{d{t}^{2}}=ay$.

```syms y(t) a eqn = diff(y,t,2) == a*y; ySol(t) = dsolve(eqn)```
```ySol(t) = C2*exp(-a^(1/2)*t) + C3*exp(a^(1/2)*t)```

### Solve Differential Equation with Condition

Specify conditions as the second input to `dsolve` by using the `==` operator. Specifying conditions eliminates arbitrary constants, such as `C1`, `C2`,`...` from the solution.

Solve the equation $\frac{dy}{dt}=ay$ with the condition $y\left(0\right)=5$.

```syms y(t) a eqn = diff(y,t) == a*y; cond = y(0) == 5; ySol(t) = dsolve(eqn,cond)```
```ySol(t) = 5*exp(a*t)```

Solve the second-order differential equation $\frac{{d}^{2}y}{d{t}^{2}}={a}^{2}y$ with two conditions, $y\left(0\right)=b$ and $y\text{'}\left(0\right)=1$. Create the second condition by assigning `diff(y,t)` to `Dy` and then using `Dy(0) == 1`.

```syms y(t) a b eqn = diff(y,t,2) == a^2*y; Dy = diff(y,t); cond = [y(0)==b, Dy(0)==1]; ySol(t) = dsolve(eqn,cond)```
```ySol(t) = (exp(a*t)*(a*b + 1))/(2*a) + (exp(-a*t)*(a*b - 1))/(2*a)```

Since two conditions are specified here, constants are eliminated from the solution of the second-order equation. In general, to eliminate constants from the solution, the number of conditions must equal the order of the equation.

### Solve System of Differential Equations

Solve a system of differential equations by specifying the equations as a vector. `dsolve` returns a structure containing the solutions.

Solve the system of equations

`$\begin{array}{l}\frac{dy}{dt}=z\\ \frac{dz}{dt}=-y.\end{array}$`
```syms y(t) z(t) eqns = [diff(y,t) == z, diff(z,t) == -y]; sol = dsolve(eqns)```
```sol = struct with fields: z: [1×1 sym] y: [1×1 sym]```

Access the solutions by addressing the elements of the structure.

`soly(t) = sol.y`
```soly(t) = C2*cos(t) + C1*sin(t)```
`solz(t) = sol.z`
```solz(t) = C1*cos(t) - C2*sin(t)```

### Assign Outputs to Functions or Variables

When solving for multiple functions, `dsolve` returns a structure by default. Alternatively, you can directly assign solutions to functions or variables by specifying the outputs explicitly as a vector. `dsolve` sorts outputs in alphabetical order using `symvar`.

Solve a system of differential equations and assign the outputs to functions.

```syms y(t) z(t) eqns = [diff(y,t)==z, diff(z,t)==-y]; [ySol(t) zSol(t)] = dsolve(eqns)```
```ySol(t) = C2*cos(t) + C1*sin(t) zSol(t) = C1*cos(t) - C2*sin(t)```

### When No Solutions Are Found

If `dsolve` cannot solve your equation, then try solving the equation numerically. See Solve a Second-Order Differential Equation Numerically.

```syms y(x) eqn = diff(y, 2) == (1 - y^2)*diff(y) - y; dsolve(eqn)```
``` Warning: Unable to find explicit solution. > In dsolve (line 201) ans = [ empty sym ]```

### Include Special Cases by Turning Off Internal Simplifications

By default, `dsolve` applies simplifications that are not generally correct, but produce simpler solutions. For details, see Algorithms. Instead, include these special cases by turning off these simplifications.

Solve $\frac{dy}{dt}=\frac{a}{\sqrt{y}}+y$ where $y\left(a\right)=1$ with and without simplifications. Turn off simplifications by setting ‘`IgnoreAnalyticConstraints`’ to `false`.

```syms a y(t) eqn = diff(y) == a/sqrt(y) + y; cond = y(a) == 1; withSimplifications = dsolve(eqn, cond)```
```withSimplifications = (exp((3*t)/2 - (3*a)/2 + log(a + 1)) - a)^(2/3)```
`withoutSimplifications = dsolve(eqn, cond, 'IgnoreAnalyticConstraints', false)`
```withoutSimplifications = piecewise(pi/2 < angle(-a), {piecewise(in(C11, 'integer'),... (- a + exp((3*t)/2 - (3*a)/2 + log(a + 1) + pi*C11*2i))^(2/3))},... angle(-a) <= -pi/2, {piecewise(in(C12, 'integer'),... (- a + exp((3*t)/2 - (3*a)/2 + log(a + 1) + pi*C12*2i))^(2/3))},... angle(-a) in Dom::Interval(-pi/2, [pi/2]), {piecewise(in(C13, 'integer'),... (- a + exp((3*t)/2 - (3*a)/2 + log(a + 1) + pi*C13*2i))^(2/3))})```

`withSimplifications` is easy to use but incomplete, while `withoutSimplifications` includes special cases but is not easy to use.

Further, for certain equations, `dsolve` cannot find an explicit solution if you set ‘`IgnoreAnalyticConstraints`’ to `false`.

## Input Arguments

collapse all

Differential equation or system of equations, specified as a symbolic equation or a vector of symbolic equations.

Specify a differential equation by using the `==` operator. In the equation, represent differentiation by using `diff`. For example, `diff(y,x)` differentiates the symbolic function `y(x)` with respect to `x`. Create the symbolic function `y(x)` by using `syms` as `syms y(x)`. Thus, to solve ```d2y(x)/dx2 = x*y(x)```, enter:

```syms y(x) dsolve(diff(y,x,2) == x*y)```

Specify a system of differential equations by using a vector of equations, such as ```dsolve([diff(y,t) == z, diff(z,t) == -y])```.

Initial or boundary condition, specified as a symbolic equation or vector of symbolic equations.

When a condition contains a derivative, represent the derivative with `diff` and assign the `diff` call to a variable. Then create conditions by using that variable. For an example, see Solve Differential Equation with Condition.

Specify multiple conditions by using a vector of equations. If the number of conditions is less than the number of dependent variables, the solutions contain arbitrary constants `C1`, `C2`,`...`.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside quotes. You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: `'IgnoreAnalyticConstraints',false` does not apply internal simplifications.

Use internal simplifications, specified as `true` or `false`.

By default, the solver applies simplifications while solving the differential equation. These simplifications might not be generally valid. Therefore, by default, the solver does not guarantee the completeness of results. For details, see Algorithms. To solve ordinary differential equations without these simplifications, set ‘`IgnoreAnalyticConstraints`’ to `false`. Results obtained with ‘`IgnoreAnalyticConstraints`’ set to `false` are correct for all values of the arguments.

If you do not set ‘`IgnoreAnalyticConstraints`’ to `false`, always verify results returned by the `dsolve` command.

Maximum degree of polynomial equations for which solver uses explicit formulas, specified as a positive integer smaller than 5. `dsolve` does not use explicit formulas when solving polynomial equations of degrees larger than `MaxDegree`.

## Output Arguments

collapse all

Solutions of differential equation, returned as a symbolic expression or a vector of symbolic expressions. The size of `S` is the number of solutions.

Variables storing solutions of differential equation, returned as a vector of symbolic variables. The number of output variables must equal the number of dependent variables in a system. `dsolve` sorts the dependent variables alphabetically, and then assigns the solutions for the variables to output variables or symbolic arrays.

## Tips

• If `dsolve` cannot find an explicit or implicit solution, then it issues a warning and returns the empty `sym`. In this case, try to find a numeric solution using the MATLAB® `ode23` or `ode45` function. Sometimes, the output is an equivalent lower-order differential equation or an integral.

• If `dsolve` cannot find a closed-form (explicit) solution, it attempts to find an implicit solution. When `dsolve` returns an implicit solution, it issues this warning:

```Warning: Explicit solution could not be found; implicit solution returned.```
• `dsolve` does not return complete solutions even if '`IgnoreAnalyticConstraints`’ is `false`.

## Algorithms

If you do not set ‘`IgnoreAnalyticConstraints`’ to `false`, then `dsolve` applies these rules while solving the equation:

• log(a) + log(b) = log(a·b) for all values of a and b. In particular, the following equality is applied for all values of a, b, and c:

(a·b)c = ac·bc.

• log(ab) = b·log(a) for all values of a and b. In particular, the following equality is applied for all values of a, b, and c:

(ab)c = ab·c.

• If f and g are standard mathematical functions and f(g(x)) = x for all small positive numbers, f(g(x)) = x is assumed to be valid for all complex x. In particular:

• log(ex) = x

• asin(sin(x)) = x, acos(cos(x)) = x, atan(tan(x)) = x

• asinh(sinh(x)) = x, acosh(cosh(x)) = x, atanh(tanh(x)) = x

• Wk(x·ex) = x for all values of k

• The solver can multiply both sides of an equation by any expression except `0`.

• The solutions of polynomial equations must be complete.