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).**

`S = dsolve(eqn)`

`S = dsolve(eqn,cond)`

`S = dsolve(eqn,cond,Name,Value)`

`[y1,...,yN] = dsolve(___)`

uses
additional options specified by one or more `S`

= dsolve(`eqn`

,`cond`

,`Name,Value`

)`Name,Value`

pair
arguments.

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.

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)

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 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)

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)

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 ]

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`

.

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`

.

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}=*a*^{c}·*b*^{c}.log(

*a*^{b}) =*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*:(

*a*^{b})^{c}=*a*^{b·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(

*e*^{x}) =*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*W

_{k}(*x*·*e*^{x}) =*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.

`functionalDerivative`

| `linsolve`

| `ode23`

| `ode45`

| `odeToVectorField`

| `solve`

| `syms`

| `vpasolve`