Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Differential equations and systems solver

**Character vector inputs are not recommended. Instead, use syms to declare variables and replace
inputs such as dsolve('Dy = y') with syms
y(t); dsolve(diff(y,t) == y).**

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. You can eliminate constants
by specifying conditions. See Solve Differential Equation with Condition.

For more examples, see Solve a Single Differential Equation. For a complex workflow involving differential equations, see Solving Partial Differential Equations.

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) = C5*exp(a^(1/2)*t) + C6*exp(-a^(1/2)*t)

`C5`

and `C6`

are constants.
You can eliminate constants by specifying conditions. See 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 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 find an
explicit or implicit solution, then `dsolve`

issues
a warning and returns an empty `sym`

. Returning an
empty symbolic object does not mean that no solutions exist.

syms y(x) eqn = exp(diff(y)) == 0; dsolve(eqn)

Warning: Explicit solution could not be found. ans = [ empty sym ]

By default, `dsolve`

applies
simplifications that are not generally correct, but produce simpler
solutions. For details, see Algorithms.
Instead, obtain complete results 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`

.

`functionalDerivative`

| `linsolve`

| `ode23`

| `ode45`

| `odeToVectorField`

| `solve`

| `syms`

| `vpasolve`

Was this topic helpful?