主要内容

求解微分方程组

使用 dsolve 函数(可以指定初始条件,也可以不指定初始条件)求解包含多个变量的由多个常微分方程组成的方程组。要求解单个微分方程,请参阅求解微分方程

求解微分方程组

求解以下一阶线性微分方程组。

dudt=3u+4v,dvdt=-4u+3v.

首先,通过使用 syms 创建符号函数 u(t)v(t),来表示 uv

syms u(t) v(t)

使用 == 定义方程,并使用 diff 函数表示微分。

ode1 = diff(u) == 3*u + 4*v;
ode2 = diff(v) == -4*u + 3*v;
odes = [ode1; ode2]
odes(t) = 

(t u(t)=3u(t)+4v(t)t v(t)=3v(t)-4u(t))

使用 dsolve 函数求解该方程组,它会以结构体的元素形式返回解。

S = dsolve(odes)
S = struct with fields:
    v: C1*cos(4*t)*exp(3*t) - C2*sin(4*t)*exp(3*t)
    u: C2*cos(4*t)*exp(3*t) + C1*sin(4*t)*exp(3*t)

如果 dsolve 无法求解方程,则尝试以数值方式求解方程。请参阅Solve a Second-Order Differential Equation Numerically

要获得 u(t)v(t),请对结构体 S 进行索引。

uSol(t) = S.u
uSol(t) = C2cos(4t)e3t+C1sin(4t)e3t
vSol(t) = S.v
vSol(t) = C1cos(4t)e3t-C2sin(4t)e3t

或者,通过提供多个输出参量直接存储 u(t)v(t)

[uSol(t),vSol(t)] = dsolve(odes)
uSol(t) = C2cos(4t)e3t+C1sin(4t)e3t
vSol(t) = C1cos(4t)e3t-C2sin(4t)e3t

由于未指定条件,因此解中出现了常数 C1C2。在指定初始条件 u(0) == 0v(0) == 0 的情况下求解该方程组。dsolve 函数会求出满足这些条件的常数的值。

cond1 = u(0) == 0;
cond2 = v(0) == 1;
conds = [cond1; cond2];
[uSol(t),vSol(t)] = dsolve(odes,conds)
uSol(t) = sin(4t)e3t
vSol(t) = cos(4t)e3t

使用 fplot 绘制解的图。

fplot(uSol)
hold on
fplot(vSol)
grid on
legend("uSol","vSol",Location="best")

Figure contains an axes object. The axes object contains 2 objects of type functionline. These objects represent uSol, vSol.

求解矩阵形式的微分方程

使用 dsolve 求解矩阵形式的微分方程。

以下面这个微分方程组为例。

dxdt=x+2y+1,dydt=-x+y+t.

该方程组的矩阵形式为

[xy]=[12-11][xy]+[1t].

Y=[xy],A=[12-11],B=[1t].

此时方程组变为 Y=A Y+B.

定义这些矩阵和矩阵方程。

syms x(t) y(t)
A = [1 2; -1 1];
B = [1; t];
Y = [x; y];
odes = diff(Y) == A*Y + B
odes(t) = 

(t x(t)=x(t)+2y(t)+1t y(t)=t-x(t)+y(t))

使用 dsolve 求解矩阵方程。使用 simplify 函数简化解。

[xSol(t),ySol(t)] = dsolve(odes);
xSol(t) = simplify(xSol(t))
xSol(t) = 

2t3+2C2etcos(2t)+2C1etsin(2t)+19

ySol(t) = simplify(ySol(t))
ySol(t) = 

C1etcos(2t)-t3-C2etsin(2t)-29

由于未指定条件,因此解中出现了常数 C1C2

在指定初始条件 u(0)=2v(0)=-1 的情况下求解该方程组。当以矩阵形式指定方程时,也必须以矩阵形式指定初始条件。dsolve 函数会求出满足这些条件的常数的值。

C = Y(0) == [2;-1];
[xSol(t),ySol(t)] = dsolve(odes,C)
xSol(t) = 

2etσ217218+e-t4σ1+2σ2+6tσ1+62tσ218-2etσ1e-t4σ2-2σ1+6tσ2-62tσ118+79where  σ1=sin(2t)  σ2=cos(2t)

ySol(t) = 

-etσ117218+e-t4σ1+2σ2+6tσ1+62tσ218-etσ2e-t4σ2-2σ1+6tσ2-62tσ118+79where  σ1=sin(2t)  σ2=cos(2t)

使用 fplot 绘制解的图。

figure
fplot(ySol)
hold on
fplot(xSol)
grid on
legend("ySol","xSol",Location="best")

Figure contains an axes object. The axes object contains 2 objects of type functionline. These objects represent ySol, xSol.

另请参阅