Solve a Single Differential Equation

Use dsolve to compute symbolic solutions to ordinary differential equations. You can specify the equations as symbolic expressions containing diff or as strings with the letter D to indicate differentiation.

    Note:   Because D indicates differentiation, the names of symbolic variables must not contain D.

Before using dsolve, create the symbolic function for which you want to solve an ordinary differential equation. Use sym or syms to create a symbolic function. For example, create a function y(x):

syms y(x)

For details, see Create Symbolic Functions.

To specify initial or boundary conditions, use additional equations. If you do not specify initial or boundary conditions, the solutions will contain integration constants, such as C1, C2, and so on.

The output from dsolve parallels the output from solve. That is, you can:

  • Call dsolve with the number of output variables equal to the number of dependent variables.

  • Place the output in a structure whose fields contain the solutions of the differential equations.

First-Order Linear ODE

Suppose you want to solve the equation y'(t) = t*y. First, create the symbolic function y(t):

syms y(t)

Now use dsolve to solve the equation:

y(t) = dsolve(diff(y,t) == t*y)
y(t) =

y(t) = C2*exp(t^2/2) is a solution to the equation for any constant C2.

Solve the same ordinary differential equation, but now specify the initial condition y(0) = 2:

syms y(t)
y(t) = dsolve(diff(y,t) == t*y, y(0) == 2)
y(t) =

Nonlinear ODE

Nonlinear equations can have multiple solutions, even if you specify initial conditions. For example, solve this equation:

syms x(t)
x(t) = dsolve((diff(x,t) + x)^2 == 1, x(0) == 0)

results in

x(t) =
 exp(-t) - 1
 1 - exp(-t)

Second-Order ODE with Initial Conditions

Solve this second-order differential equation with two initial conditions. One initial condition is a derivative y'(x) at x = 0. To be able to specify this initial condition, create an additional symbolic function Dy = diff(y). (You also can use any valid function name instead of Dy.) Then Dy(0) = 0 specifies that Dy = 0 at x = 0.

syms y(x)
Dy = diff(y);
y(x) = dsolve(diff(y, x, x) == cos(2*x) - y, y(0) == 1, Dy(0) == 0);
y(x) = simplify(y)
y(x) =
1 - (8*sin(x/2)^4)/3

Third-Order ODE

Solve this third-order ordinary differential equation:


u(0)=1, u(0)=1, u(0)=π,

Because the initial conditions contain the first- and the second-order derivatives, create two additional symbolic functions, Dy and D2y to specify these initial conditions:

syms u(x)
Du = diff(u, x);
D2u = diff(u, x, 2);
u(x) = dsolve(diff(u, x, 3) == u, u(0) == 1, Du(0) == -1, D2u(0) == pi)
u(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. The last example is the Airy differential equation, whose solution is called the Airy function.

Differential Equation

MATLAB® Command


y(0) = 1

syms y(t)
dsolve(diff(y) + 4*y == exp(-t), y(0) == 1)

2x2y′′ + 3xy′ – y = 0
( ′ = d/dx)

syms y(x)
dsolve(2*x^2*diff(y, 2) + 3*x*diff(y) - y == 0)


y(0)=0, y(3)=1πK1/3(23)

(The Airy equation)

syms y(x)
dsolve(diff(y, 2) == x*y, y(0) == 0, y(3) == besselk(1/3, 2*sqrt(3))/pi)

See Also

Was this topic helpful?