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.
First, represent and 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) =
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) =
vSol(t) = S.v
vSol(t) =
Alternatively, store u(t) and v(t) directly by providing multiple output arguments.
[uSol(t),vSol(t)] = dsolve(odes)
uSol(t) =
vSol(t) =
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) =
vSol(t) =
Visualize the solution using fplot.
fplot(uSol) hold on fplot(vSol) grid on legend("uSol","vSol",Location="best")

Solve Differential Equations in Matrix Form
Solve differential equations in matrix form by using dsolve.
Consider this system of differential equations.
The matrix form of the system is
Let
The system is now .
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) =
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) =
ySol(t) = simplify(ySol(t))
ySol(t) =
The constants C1 and C2 appear because no conditions are specified.
Solve the system with the initial conditions and . 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) =
ySol(t) =
Visualize the solution using fplot.
figure fplot(ySol) hold on fplot(xSol) grid on legend("ySol","xSol",Location="best")
