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!

  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 20, 2020 at 19:17
  • 1
    $\begingroup$ Thanks, I will try that! $\endgroup$ May 20, 2020 at 22:35
  • $\begingroup$ @MarcoB Answer updated! $\endgroup$ May 21, 2020 at 5:32

3 Answers 3


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}]

Mathematica graphics

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}};

(* {-3, 2} *)

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

  • $\begingroup$ 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! $\endgroup$ May 20, 2020 at 22:41
  • 1
    $\begingroup$ @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. $\endgroup$
    – Nasser
    May 20, 2020 at 22:46
  • $\begingroup$ It makes sense now. Thanks! $\endgroup$ 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}};
DSolve[X'[t] == A.X[t],X[t],t]
  • 1
    $\begingroup$ Glad to see this! (+1) $\endgroup$
    – MarcoB
    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}
  • $\begingroup$ Thanks for sharing this. It's really clever! $\endgroup$ May 21, 2020 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.