Main Content

Solve a System of Differential Equations

Solve a system of several ordinary differential equations in several variables by using the dsolve function, with or without initial conditions. To solve a single differential equation, see Solve Differential Equation.

Solve System of Differential Equations

Solve this system of linear first-order differential equations.

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

First, represent u and v by using syms to create the symbolic functions u(t) and v(t).

syms u(t) v(t)

Define the equations using == and represent differentiation using the diff function.

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))

Solve the system using the dsolve function which returns the solutions as elements of a structure.

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)

If dsolve cannot solve your equation, then try solving the equation numerically. See Solve a Second-Order Differential Equation Numerically.

To access u(t) and v(t), index into the structure 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

Alternatively, store u(t) and v(t) directly by providing multiple output arguments.

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

The constants C1 and C2 appear because no conditions are specified. Solve the system with the initial conditions u(0) == 0 and v(0) == 0. The dsolve function finds values for the constants that satisfy these conditions.

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

Visualize the solution using 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.

Solve Differential Equations in Matrix Form

Solve differential equations in matrix form by using dsolve.

Consider this system of differential equations.

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

The matrix form of the system is

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

Let

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

The system is now Y=AY+B..

Define these matrices and the matrix equation.

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))

Solve the matrix equation using dsolve. Simplify the solution by using the simplify function.

[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

The constants C1 and C2 appear because no conditions are specified.

Solve the system with the initial conditions u(0)=2 and v(0)=-1. When specifying equations in matrix form, you must specify initial conditions in matrix form too. dsolve finds values for the constants that satisfy these conditions.

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)

Visualize the solution using fplot.

clf
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.

See Also