Main Content

Solve Differential Equation

通过使用dsolvefunction, with or without initial conditions. To solve a system of differential equations, seeSolve a System of Differential Equations.

First-Order Linear ODE

Solve this differential equation.

d y d t = t y .

First, representyby usingsymsto create the symbolic functiony(t).

syms y(t)

Define the equation using==并代表使用difffunction.

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

Solve the equation usingdsolve.

ySol(t) = dsolve(ode)
ySol(t) = C1*exp(t^2/2)

解微分方程的条件

In the previous solution, the constantC1出现是因为没有指定条件。用初始条件求解方程y(0) == 2. Thedsolvefunction finds a value ofC1满足条件。

cond = y(0) == 2; ySol(t) = dsolve(ode,cond)
ySol(t) = 2*exp(t^2/2)

Ifdsolvecannot solve your equation, then try solving the equation numerically. SeeSolve 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.

( d y d t + y ) 2 = 1 , y ( 0 ) = 0。

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)

Second-Order ODE with Initial Conditions

Solve this second-order differential equation with two initial conditions.

d 2 y d x 2 = cos ( 2 x ) y , y ( 0 ) = 1 , y ' ( 0 ) = 0。

Define the equation and conditions. The second initial condition involves the first derivative ofy. Represent the derivative by creating the symbolic functionDy = diff(y)和then define the condition usingDy(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;

Solveodefory. Simplify the solution using the简化function.

conds = [cond1 cond2]; ySol(x) = dsolve(ode,conds); ySol = simplify(ySol)
ySol(x) = 1 - (8*sin(x/2)^4)/3

Third-Order ODE with Initial Conditions

Solve this third-order differential equation with three initial conditions.

d 3 u d x 3 = u , u ( 0 ) = 1 , u ( 0 ) = 1 , u ( 0 ) = π .

Because the initial conditions contain the first- and second-order derivatives, create two symbolic functions,Du = diff(u,x)d2u = diff(u,x,2), 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. The last example is the Airy differential equation, whose solution is called the Airy function.

Differential Equation

MATLAB®Commands

d y d t + 4 y ( t ) = e t , y ( 0 ) = 1.

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 d 2 y d x 2 + 3 x d y d x 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) = C2/(3*x) + C3*x^(1/2)

The Airy equation.

d 2 y d x 2 = x y ( 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)

See Also