Вы находитесь на странице: 1из 2

Solve a Single Differential Equation

Use dsolve to compute symbolic solutions to ordinary differential equations. You can specify the equations as
symbolic expressions containing dif 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 functiony(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(dif(y,t) == 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(dif(y,t) == t*y, y(0) == 2)
y(t) =
2*exp(t^2/2)
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((dif(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 =

dif(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 = dif(y);
y(x) = dsolve(dif(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:
3

d udx =u
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 = dif(u, x);
D2u = dif(u, x, 2);
u(x) = dsolve(dif(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
+4y(t)=et

MATLAB Command

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

dydt

y(0) = 1
2x y + 3xy y = 0
( = d/dx)

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

d ydx =xy(x)
y(0)=0, y(3)= K

1 1/3

(The Airy equation)

(2G3)

syms y(x)
dsolve(dif(y, 2) == x

Вам также может понравиться