Solve Differential Equation
Solve a differential equation analytically by using the dsolve
function, with or without initial conditions. To solve a system of differential equations, see
Solve a System of Differential Equations.
FirstOrder Linear ODE
Solve this differential equation.
$$\frac{dy}{dt}=ty.$$
First, represent y by using syms
to create the
symbolic function y(t)
.
syms y(t)
Define the equation using ==
and represent differentiation using the
diff
function.
ode = diff(y,t) == t*y
ode(t) = diff(y(t), t) == t*y(t)
Solve the equation using dsolve
.
ySol(t) = dsolve(ode)
ySol(t) = C1*exp(t^2/2)
Solve Differential Equation with Condition
In the previous solution, the constant C1
appears because no condition
was specified. Solve the equation with the initial condition y(0) == 2
. The
dsolve
function finds a value of C1
that satisfies
the condition.
cond = y(0) == 2; ySol(t) = dsolve(ode,cond)
ySol(t) = 2*exp(t^2/2)
If dsolve
cannot solve your
equation, then try solving the equation numerically. See Solve a SecondOrder Differential Equation Numerically.
Nonlinear Differential Equation with Initial Condition
Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.
$$\begin{array}{l}{\left(\frac{dy}{dt}+y\right)}^{2}=1,\\ y\left(0\right)=0.\end{array}$$
syms y(t) ode = (diff(y,t)+y)^2 == 1; cond = y(0) == 0; ySol(t) = dsolve(ode,cond)
ySol(t) = exp(t)  1 1  exp(t)
SecondOrder ODE with Initial Conditions
Solve this secondorder differential equation with two initial conditions.
$$\begin{array}{l}\frac{{d}^{2}y}{d{x}^{2}}=\mathrm{cos}\left(2x\right)y,\\ y\left(0\right)=1,\\ y\text{'}\left(0\right)=0.\end{array}$$
Define the equation and conditions. The second initial condition involves the first
derivative of y
. Represent the derivative by creating the symbolic function
Dy = diff(y)
and then define the condition using
Dy(0)==0
.
syms y(x) Dy = diff(y); ode = diff(y,x,2) == cos(2*x)y; cond1 = y(0) == 1; cond2 = Dy(0) == 0;
Solve ode
for y
. Simplify the solution using the
simplify
function.
conds = [cond1 cond2]; ySol(x) = dsolve(ode,conds); ySol = simplify(ySol)
ySol(x) = 1  (8*sin(x/2)^4)/3
ThirdOrder ODE with Initial Conditions
Solve this thirdorder differential equation with three initial conditions.
$$\begin{array}{l}\frac{{d}^{3}u}{d{x}^{3}}=u,\\ u(0)=1,\text{\hspace{0.17em}}\\ {u}^{\prime}(0)=1,\\ \text{\hspace{0.17em}}{{u}^{\prime}}^{\prime}(0)=\pi .\end{array}$$
Because the initial conditions contain the first and secondorder derivatives, create two
symbolic functions, Du = diff(u,x)
and D2u =
diff(u,x,2)
, to specify the initial conditions.
syms u(x) Du = diff(u,x); D2u = diff(u,x,2);
Create the equation and initial conditions, and solve it.
ode = diff(u,x,3) == u; cond1 = u(0) == 1; cond2 = Du(0) == 1; cond3 = D2u(0) == pi; conds = [cond1 cond2 cond3]; uSol(x) = dsolve(ode,conds)
uSol(x) = (pi*exp(x))/3  exp(x/2)*cos((3^(1/2)*x)/2)*(pi/3  1) ... (3^(1/2)*exp(x/2)*sin((3^(1/2)*x)/2)*(pi + 1))/3
More ODE Examples
This table shows examples of differential equations and their Symbolic Math Toolbox™ syntax.
Differential Equation  MATLAB^{®} Commands 

$$\begin{array}{l}\frac{dy}{dt}+4y(t)={e}^{t},\\ y\left(0\right)=1.\end{array}$$

syms y(t) ode = diff(y)+4*y == exp(t); cond = y(0) == 1; ySol(t) = dsolve(ode,cond) ySol(t) = exp(t)/3 + (2*exp(4*t))/3 
$$2{x}^{2}\frac{{d}^{2}y}{d{x}^{2}}+3x\frac{dy}{dx}y=0.$$

syms y(x) ode = 2*x^2*diff(y,x,2)+3*x*diff(y,x)y == 0; ySol(x) = dsolve(ode) ySol(x) = C1/(3*x) + C2*x^(1/2) 
The Airy equation. $$\frac{{d}^{2}y}{d{x}^{2}}=xy(x).$$ 
syms y(x) ode = diff(y,x,2) == x*y; ySol(x) = dsolve(ode) ySol(x) = C1*airy(0,x) + C2*airy(2,x) 
Puiseux series solution. $$\left({x}^{2}+1\right)\frac{{d}^{2}y}{d{x}^{2}}2x\frac{dy}{dx}+y=0.$$ 
syms y(x) a ode = (x^2+1)*diff(y,x,2)2*x*diff(y,x)+y == 0; Dy = diff(y,x); cond = [Dy(0) == a; y(0) == 5]; ySol(x) = dsolve(ode,cond,'ExpansionPoint',0) ySol(x) =  (a*x^5)/120  (5*x^4)/24 + (a*x^3)/6  (5*x^2)/2 + a*x + 5 
See Also
dsolve
 odeFunction
 odeToVectorField
 reduceDifferentialOrder
 daeFunction