Solve system of differential equations
Support for character vector or string inputs will be removed in a future release. Instead,
syms to declare variables and replace inputs
dsolve('Dy = y') with
syms y(t); dsolve(diff(y,t) ==
Solve the first-order differential equation
Specify the first-order derivative by using
diff and the equation by using ==. Then, solve the equation by using
syms y(t) a eqn = diff(y,t) == a*y; S = dsolve(eqn)
Solve the second-order differential equation
Specify the second-order derivative of y by using
diff(y,t,2) and the equation by using
==. Then, solve the equation by using
syms y(t) a eqn = diff(y,t,2) == a*y; ySol(t) = dsolve(eqn)
Solve the first-order differential equation with the initial condition .
Specify the initial condition as the second input to
dsolve by using the
== operator. Specifying condition eliminates arbitrary constants, such as
..., from the solution.
syms y(t) a eqn = diff(y,t) == a*y; cond = y(0) == 5; ySol(t) = dsolve(eqn,cond)
Next, solve the second-order differential equation with the initial conditions and .
Specify the second initial condition by assigning
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)
This second-order differential equation has two specified conditions, so constants are eliminated from the solution. In general, to eliminate constants from the solution, the number of conditions must equal the order of the equation.
Solve the system of differential equations
Specify the system of equations as a vector.
dsolve returns a structure containing the solutions.
syms y(t) z(t) eqns = [diff(y,t) == z, diff(z,t) == -y]; S = dsolve(eqns)
S = struct with fields: z: [1x1 sym] y: [1x1 sym]
Access the solutions by addressing the elements of the structure.
ySol(t) = S.y
zSol(t) = S.z
When solving for multiple functions,
dsolve returns a structure by default. Alternatively, you can assign solutions to functions or variables directly by explicitly specifying the outputs as a vector.
dsolve sorts the outputs in alphabetical order using
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)
dsolve cannot solve a differential equation analytically, then it returns an empty symbolic array. You can solve the differential equation by using MATLAB® numerical solver, such as
ode45. For more information, see Solve a Second-Order Differential Equation Numerically.
syms y(x) eqn = diff(y, 2) == (1 - y^2)*diff(y) - y; S = dsolve(eqn)
Warning: Unable to find explicit solution.
S = [ empty sym ]
Solve the differential equation with condition . By default,
dsolve applies simplifications that are not generally correct, but produce simpler solutions. For more details, see Algorithms.
syms a y(t) eqn = diff(y) == a/sqrt(y) + y; cond = y(a) == 1; ySimplified = dsolve(eqn, cond)
To return the solutions that include all possible values of the parameter , turn off simplifications by setting
yNotSimplified = dsolve(eqn,cond,'IgnoreAnalyticConstraints',false)
Solve the differential equation without specifying the initial condition.
syms y(x) eqn = diff(y) == exp(-1/x)/x^2; ySol(x) = dsolve(eqn)
To eliminate constants from the solution, specify the initial condition
cond = y(0) == 1; S = dsolve(eqn,cond)
The function in the solution
ySol(x) has different one-sided limits at . The function has a right-side limit, , but it has undefined left-side limit, .
When you specify the condition
y(x0) for a function with different one-sided limits at
dsolve treats the condition as a limit from the right .
eqn— Differential equation or system of equations
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
y(x) with respect to
the symbolic function
y(x) by using
syms and solve the equation
syms y(x) dsolve(diff(y,x,2) == x*y)
Specify a system of differential equations by using a vector of equations, as in
dsolve([diff(y,t) == z, diff(z,t) == -y]).
cond— Initial or boundary condition
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. Assign the
diff call to a variable and use the
variable to specify the condition. For example, see Solve Differential Equations with Conditions.
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 the arbitrary
comma-separated pairs of
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
'IgnoreAnalyticConstraints',falsedoes not apply internal simplifications.
'IgnoreAnalyticConstraints'— Option to use internal simplifications
Option to use internal simplifications, specified as
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. If
verify results returned by the
dsolve function. For more details,
To solve ordinary differential equations without these simplifications, set
'IgnoreAnalyticConstraints' set to
false are correct for all values of the arguments. For certain
dsolve might not return an explicit solution if you set
'MaxDegree'— Maximum degree of polynomial equation for which solver uses explicit formulas
Maximum degree of polynomial equations for which the 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
S— Solutions of differential equation
Solutions of differential equation, returned as a symbolic expression or a vector of symbolic
expressions. The size of
S is the number of solutions.
y1,...,yN— Variables storing solutions of differential equations
Variables storing solutions of differential equations, returned as a vector of
symbolic variables. The number of output variables must equal the number of dependent
variables in a system of equations.
dsolve sorts the dependent
variables alphabetically, and then assigns the solutions for the variables to output
variables or symbolic arrays.
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 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®
ode45 function. Sometimes, the output is an
equivalent lower-order differential equation or an integral.
dsolve does not always return complete solutions even if
dsolve returns a function that has different one-sided limits at
x0 and you specify the condition
dsolve treats the condition as a limit from the right, .
If you do not set
dsolve applies these rules while solving
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 branch indices k of the Lambert W function.
The solver can multiply both sides of an equation
by any expression except
The solutions of polynomial equations must be complete.
Warns starting in R2019b
dsolve will not accept equations as strings or character vectors in a
future release. Instead, use symbolic expressions or
sym objects to
define differential equations. For example, replace inputs such as
syms y(t); dsolve(diff(y,t) == y).