# dsolve

Ordinary differential equation and system solver

## Syntax

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

## Description

`S = dsolve(eqn)` solves the ordinary differential equation `eqn`. Here `eqn` is a symbolic equation containing `diff` to indicate derivatives. Alternatively, you can use a string with the letter `D` indicating derivatives. For example, ```syms y(x); dsolve(diff(y) == y + 1)``` and `dsolve('Dy = y + 1','x')` both solve the equation `dy/dx = y + 1` with respect to the variable `x`. Also, `eqn` can be an array of such equations or strings.

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

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

`Y = dsolve(eqns)` solves the system of ordinary differential equations `eqns` and returns a structure array that contains the solutions. The number of fields in the structure array corresponds to the number of independent variables in the system.

`Y = dsolve(eqns,conds)` solves the system of ordinary differential equations `eqns` with the initial or boundary conditions `conds`.

`Y = dsolve(eqns,conds,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.

`[y1,...,yN] = dsolve(eqns)` solves the system of ordinary differential equations `eqns` and assigns the solutions to the variables `y1,...,yN`.

`[y1,...,yN] = dsolve(eqns,conds)` solves the system of ordinary differential equations `eqns` with the initial or boundary conditions `conds`.

`[y1,...,yN] = dsolve(eqns,conds,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.

## Input Arguments

 `eqn` Symbolic equation, string representing an ordinary differential equation, or array of symbolic equations or strings. When representing `eqn` as a symbolic equation, you must create a symbolic function, for example `y(x)`. Here `x` is an independent variable for which you solve an ordinary differential equation. Use the `==` operator to create an equation. Use the `diff` function to indicate differentiation. For example, to solve ```d2y(x)/dx2 = x*y(x)```, enter: ```syms y(x) dsolve(diff(y, 2) == x*y)``` When representing `eqn` as a string, use the letter `D` to indicate differentiation. By default, `dsolve` assumes that the independent variable is `t`. Thus, `Dy` means `dy/dt`. You can specify the independent variable. The letter `D` followed by a digit indicates repeated differentiation. Any character immediately following a differentiation operator is a dependent variable. For example, to solve `y''(x) = x*y(x)`, enter: `dsolve('D2y = x*y','x')` or `dsolve('D2y == x*y','x')` `cond` Equation or string representing an initial or boundary condition. If you use equations, assign expressions with `diff` to some intermediate variables. For example, use `Dy`, `D2y`, and so on as intermediate variables: ```Dy = diff(y); D2y = diff(y, 2);``` Then define initial conditions using symbolic equations, such as `y(a) == b` and `Dy(a) == b`. Here `a` and `b` are constants. If you represent initial and boundary conditions as strings, you do not need to create intermediate variables. In this case, follow the same rules as you do when creating an equation `eqn` as a string. For example, specify `'y(a) = b'` and ```'Dy(a) = b'```. When using strings, you can use `=` or `==` in equations. `eqns` Symbolic equations or strings separated by commas and representing a system of ordinary differential equations. Each equation or string represents an ordinary differential equation. `conds` Symbolic equations or strings separated by commas and representing initial or boundary conditions or both types of conditions. Each equation or string represents an initial or boundary condition. If the number of the specified conditions is less than the number of dependent variables, the resulting 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 single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

 `'IgnoreAnalyticConstraints'` By default, the solver applies the purely algebraic simplifications to the expressions on both sides of equations. These simplifications might not be generally valid. Therefore, by default the solver does not guarantee general correctness and completeness of the results. To solve ordinary differential equations without additional assumptions, set `IgnoreAnalyticConstraints` to `false`. The 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. Default: `true` `'MaxDegree'` Do not use explicit formulas that involve radicals when solving polynomial equations of degrees larger than the specified value. This value must be a positive integer smaller than 5. Default: 2

## Output Arguments

 `S` Symbolic array that contains solutions of an equation. The size of a symbolic array corresponds to the number of the solutions. `Y` Structure array that contains solutions of a system of equations. The number of fields in the structure array corresponds to the number of independent variables in a system. `y1,...,yN` Variables to which the solver assigns the solutions of a system of equations. The number of output variables or symbolic arrays must equal the number of independent variables in a system. The toolbox sorts independent variables alphabetically, and then assigns the solutions for these variables to output variables or symbolic arrays.

## Examples

Solve these ordinary differential equations. Use `==` to create an equation, and `diff` to indicate differentiation:

```syms a x(t) dsolve(diff(x) == -a*x)```
```ans = C2*exp(-a*t)```
```syms f(t) dsolve(diff(f) == f + sin(t))```
```ans = C5*exp(t) - (2^(1/2)*cos(t - pi/4))/2```

Solve this ordinary differential equation with the initial condition `y(0) = b`:

```syms a b y(t) dsolve(diff(y) == a*y, y(0) == b)```

Specifying the initial condition lets you eliminate arbitrary constants, such as `C1`, `C2`,`...`:

```ans = b*exp(a*t)```

Solve this ordinary differential equation with the initial and boundary conditions. To specify a condition that contains a derivative, assign the derivative to a variable:

```syms a y(t) Dy = diff(y); dsolve(diff(y, 2) == -a^2*y, y(0) == 1, Dy(pi/a) == 0)```

Because the equation contains the second-order derivative `d2y/dt2`, specifying two conditions lets you eliminate arbitrary constants in the solution:

```ans = exp(-a*t*1i)/2 + exp(a*t*1i)/2```

Solve this system of ordinary differential equations:

```syms x(t) y(t) z = dsolve(diff(x) == y, diff(y) == -x)```

When you assign the solution of a system of equations to a single output, `dsolve` returns a structure containing the solutions:

```z = y: [1x1 sym] x: [1x1 sym]```

To see the results, enter `z.x` and `z.y`:

`z.x`
```ans = C12*cos(t) + C11*sin(t)```
`z.y`
```ans = C11*cos(t) - C12*sin(t)```

By default, the solver applies a set of purely algebraic simplifications that are not correct in general, but that can produce simple and practical solutions:

```syms a y(t) dsolve(diff(y) == a/sqrt(y) + y, y(a) == 1)```
```ans = (exp((3*t)/2 - (3*a)/2 + log(a + 1)) - a)^(2/3)```

To obtain complete and generally correct solutions, set the value of `IgnoreAnalyticConstraints` to `false`:

`dsolve(diff(y) == a/sqrt(y) + y, y(a) == 1, 'IgnoreAnalyticConstraints', false)`
```Warning: The solutions are subject to the following conditions: PI/2 < angle(-a) in(C22, 'integer') . ans = (- a + exp((3*t)/2 - (3*a)/2 + log(a + 1) + pi*C22*2i))^(2/3)```

If you apply algebraic simplifications, you can get explicit solutions for some equations for which the solver cannot compute them using strict mathematical rules:

```syms y(t) dsolve(sqrt(diff(y)) == sqrt(y) + log(y^2))```
```Warning: Explicit solution could not be found; implicit solution returned. ans = 16*lambertw(0, 1/4)^2 16*lambertw(0, -1/4)^2 solve(int(1/(log(y^2) + y^(1/2))^2, y,... 'IgnoreSpecialCases', true,... 'IgnoreAnalyticConstraints', true) - t - C27 == 0, y)```

versus

`dsolve(sqrt(diff(y)) == sqrt(y) + log(y^2), 'IgnoreAnalyticConstraints', false)`
```Warning: Explicit solution could not be found; implicit solution returned. ans = solve(log(y^2) + y^(1/2) == 0, y) solve(int(1/(log(y^2) + y^(1/2))^2, y,... 'IgnoreSpecialCases', true) - t - C32 == 0, y)```

When you solve a higher-order polynomial equation, the solver sometimes uses `RootOf` to return the results:

```syms a y(x) dsolve(diff(y) == a/(y^2 + 1))```
```Warning: Explicit solution could not be found; implicit solution returned. ans = RootOf(z^3 + 3*z - 3*a*x - 3*C36, z)```

To get an explicit solution for such equations, try calling the solver with `MaxDegree`. The option specifies the maximum degree of polynomials for which the solver tries to return explicit solutions. The default value is 2. By increasing this value, you can get explicit solutions for higher-order polynomials. For example, increase the value of `MaxDegree` to 4 and get explicit solutions instead of `RootOf` for this equation:

```s = dsolve(diff(y) == a/(y^2 + 1), 'MaxDegree', 4); pretty(s)```
```/ 1 \ | #1 - -- | | #1 | | | | / 1 \ | | sqrt(3) | -- + #1 | 1i | | \ #1 / 1 #1 | | ---------------------- + ---- - -- | | 2 2 #1 2 | | | | / 1 \ | | sqrt(3) | -- + #1 | 1i | | 1 \ #1 / #1 | | ---- - ---------------------- - -- | \ 2 #1 2 2 / where / / 2 \ \1/3 | 3 C39 3 a x | 9 (C39 + a x) | | #1 == | ----- + ----- + sqrt| -------------- + 1 | | \ 2 2 \ 4 / /```

If `dsolve` can find neither an explicit nor an implicit solution, then it issues a warning and returns the empty `sym`:

```syms y(x) dsolve(exp(diff(y)) == 0)```
``` Warning: Explicit solution could not be found. ans = [ empty sym ]```

Returning the empty symbolic object does not prove that there are no solutions.

Solve this equation specifying it as a string. By default, `dsolve` assumes that the independent variable is `t`:

`dsolve('Dy^2 + y^2 == 1')`
```ans = 1 -1 cosh(C49 + t*1i) cosh(C45 - t*1i)```

Now solve this equation with respect to the variable `s`:

`dsolve('Dy^2 + y^2 == 1','s')`
```ans = 1 -1 cosh(C57 + s*1i) cosh(C53 - s*1i)```

collapse all

### Tips

• The names of symbolic variables used in differential equations should not contain the letter `D` because `dsolve` assumes that `D` is a differential operator and any character immediately following `D` is a dependent variable.

• 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.```
• If `dsolve` can find neither an explicit nor an 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. In some cases, the output is an equivalent lower-order differential equation or an integral.

### Algorithms

If you do not set the value of `IgnoreAnalyticConstraints` to `false`, the solver applies these rules to the expressions on both sides of an equation:

• log(a) + log(b) = log(a·b) for all values of a and b. In particular, the following equality is valid 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 valid 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.