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.
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):
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.
Suppose you want to solve the equation y'(t) = t*y. First, create the symbolic function y(t):
Now use dsolve to solve the equation:
y(t) = dsolve(diff(y) == t*y)
y(t) = C2*exp(t^2/2)
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*y, y(0) == 2)
y(t) = 2*exp(t^2/2)
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) + x)^2 == 1, x(0) == 0)
x(t) = exp(-t) - 1 1 - exp(-t)
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, 2) == cos(2*x) - y, y(0) == 1, Dy(0) == 0); y(x) = simplify(y)
y(x) = 1 - (8*sin(x/2)^4)/3
Solve this third-order ordinary differential equation:
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); D2u = diff(u, 2); u(x) = dsolve(diff(u, 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
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.
y(0) = 1
+ 3xy′ – y =
(The Airy equation)