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.
First-Order Linear ODE
Solve this differential equation.
First, represent 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) =
Solve the equation using dsolve
.
ySol(t) = dsolve(ode)
ySol(t) =
Solve Differential Equation with Condition
In the previous solution, the constant appears because no condition was specified. Solve the equation with the initial condition y(0) == 2
. The dsolve
function finds a value of that satisfies the condition.
cond = y(0) == 2; ySol(t) = dsolve(ode,cond)
ySol(t) =
If dsolve
cannot solve your equation, then try solving the equation numerically. See Solve a Second-Order Differential Equation Numerically.
Nonlinear Differential Equation with Initial Condition
Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.
syms y(t)
ode = (diff(y,t)+y)^2 == 1;
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
ySol(t) =
Second-Order ODE with Initial Conditions
Solve this second-order differential equation with two initial conditions.
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) =
Third-Order ODE with Initial Conditions
Solve this third-order differential equation with three initial conditions.
Because the initial conditions contain the first- and second-order 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) =
More ODE Examples
This table shows examples of differential equations and their Symbolic Math Toolbox™ syntax.
Differential Equation | Commands Using Symbolic Math Toolbox |
---|---|
| |
| |
The Airy equation. |
|
Puiseux series solution. |
|
See Also
dsolve
| odeFunction
| odeToVectorField
| reduceDifferentialOrder
| daeFunction