# How To Generate a Direction Field and Solve a System of Differential Equation

I want to draw a direction field and solve this system of differential equations using Mathematica, but I've been researching, and I can't find a way to do this:

$$\mathbf{x'} = \begin{pmatrix} 1 & 1\\ 4 & -2 \end{pmatrix} \mathbf{x}$$

Note: I'm still a newbie to Mathematica, so please be patient with me. Thanks!

• You could start by presenting your equations explicitly, rather than in matrix form (I am not sure how to interpret them). Then study the documentation of DSolve and give it a try, then report back with your code attempts. Commented May 20, 2020 at 19:17
• Thanks, I will try that! Commented May 20, 2020 at 22:35
• @MarcoB Answer updated! Commented May 21, 2020 at 5:32

I want to draw a direction field and solve this system of differential equations

Like this

f1 = x1 + x2
f2 = 4* x1 - 2 x2;
StreamPlot[{f1, f2}, {x1, -3, 3}, {x2, -3, 3}]


The x axis is x1 and the y axis is x2. These are the two state variables. From the phase plot, it looks like origin is saddle point. To verify, look at eigenvalues

A = {{1, 1}, {4, -2}};
Eigenvalues[A]

(* {-3, 2} *)


Yes, saddle point. One eigenvalue is stable and the other is not stable.

• Can you explain why do we use StreamPlot instead of dfield in this case? When should we use one over the other? And thanks for your answer, that's what I'm looking for -- I really appreciate it! Commented May 20, 2020 at 22:41
• @TuanDucVu dfield is matlab's and StreamPlot is what I use in Mathematica. They are used to make phase plot, i.e plotting one state variable against another. So when using Mathematica you can use StreamPlot to make phase plots. I use it for all my school hw's myself. Easy to use. Commented May 20, 2020 at 22:46
• It makes sense now. Thanks! Commented May 21, 2020 at 4:43

Update: I finally got a hang of how to solve the system after reading the documentation of DSolve. Here is a snippet of code:

A = {{1,1},{4,-2}};
X[t_]={x[t],y[t]};
DSolve[X'[t] == A.X[t],X[t],t]

• Glad to see this! (+1) Commented May 21, 2020 at 6:04

Since you've now figured out how to use DSolve[], allow me to present another tool you can use.

As you might be aware, the one-dimensional first order ODE

$$y^\prime(t)=a y(t)$$

has a solution that can be expressed as $$y(t)=C\exp(a t)$$, where $$C$$ is determined through an appropriate initial condition.

In complete analogy, a first order ODE with matrix coefficient $$\mathbf A$$:

$$\mathbf y^\prime(t)=\mathbf A\mathbf y(t)$$

can be expressed in terms of the matrix exponential. Mathematica has this built-in as MatrixExp[], so that the supposed result of

DSolve[{X'[t] == {{1, 1}, {4, -2}}.X[t], X[0] == {C[1], C[2]}}, X[t], t]


can in fact be immediately written down as

y[t_] = MatrixExp[{{1, 1}, {4, -2}} t, {C[1], C[2]}]


and you can easily check that this satisfies the ODE:

D[%, t] - {{1, 1}, {4, -2}}.% // Simplify
{0, 0}

• Thanks for sharing this. It's really clever! Commented May 21, 2020 at 18:02