Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to plot the graph of the direction field for a differential equation, to get a feel for it. I'm a novice, right now, when it comes to plotting in Mathematica, so I'm hoping that someone can provide a fairly easy to understand and thorough explanation. My hope is that I will become fairly proficient at understanding plotting in Mathematica, as well as differential equations. I'm a little more familiar with differential equations, but very far from what I'd consider to be an expert.

I do have an equation in mind, taken from this question from Math.SE:


I ran DSolve on it, and after a minute it was unable to evaluate the function. So perhaps this could make for an interesting exploration for others as well. I'm wondering what experience in Mathematica has taught others about what can be done in Mathematica - I'm hoping someone can offer some useful tips and demonstrations.

I'm really interested in learning about what can be done with differential equations, so I think that other equations will suffice if they serve as a better example.

share|improve this question
up vote 14 down vote accepted

For a first sketch of the direction field you might use StreamPlot:

f[x_, y_] = (y + E^x)/(x + E^y)

StreamPlot[{1, f[x, y]}, {x, 1, 6}, {y, -20, 5}, Frame -> False, 
 Axes -> True, AspectRatio -> 1/GoldenRatio]

Mathematica graphics

share|improve this answer

If you wish to explore the solutions to an equation I'd suggest the EquationTrekker package. Have a look at the documentation.

EquationTrekker[y'[x] == (y[x] + Exp[x])/(x + Exp[y[x]]), y, {x, -5, 5}]


share|improve this answer

No need to solve the differential equation to generate a direction field. According to the Wikipedia lemma on slope fields you can plot the vector {1, (y + Exp[x])/(x + Exp[y])}:

VectorPlot[{1, (y + Exp[x])/(x + Exp[y])}, {x, 0, 2}, {y, 0, 2}]

Mathematica graphics

or perhaps you could use a stream plot:

StreamPlot[{1, (y + Exp[x])/(x + Exp[y])}, {x, 0, 2}, {y, 0, 2}]

Mathematica graphics

share|improve this answer

Here is something you can do quickly and gives a nice understanding of the behavior for positive and negative initial conditions:

s[r_?NumericQ] := 
               NDSolve[{D[y[x], x] == (y[x] + E^x)/(x + E^y[x]), y[0] == r}, y, {x, 0, 5}]

  y[x] /. s[#] & /@ Union[Range[-2, 0, .1], Range[-.10, 10, 1]]], {x, 0, 15}, 
  PlotRange -> Full]

Mathematica graphics

share|improve this answer

I approached this slightly differently, in an attempt to mimic what's written in a textbook of mine for direction fields. I set the plot as a system of unit vectors, with x and y from this derivation:

$$\mathit{x}^2+\mathit{y}^2=1\wedge \mathit{m}=\frac{y}{\mathit{x}}\Rightarrow \\ \\ \mathit{x}=\frac{\mathit{y}}{\mathit{m}}\Rightarrow \\ \frac{\mathit{y}^2}{\mathit{m}^2}+\mathit{y}^2=1\Rightarrow \\ \mathit{y}=\frac{\mathit{m}}{\sqrt{\mathit{m}^2+1}} $$
$$ \\ \mathit{y}=\mathit{m} \mathit{x}\Rightarrow \\ \mathit{m}^2 \mathit{x}^2+\mathit{x}^2=1\Rightarrow \\ \mathit{x}=\frac{1}{\sqrt{\mathit{m}^2+1}} $$
So I put this into Mathematica

func =
      {1/Sqrt[1 + m[x, y]^2], m[x, y]/Sqrt[1 + m[x, y]^2]}, 
      {x, -4, 4}, {y, -4, 4},
      VectorPoints -> Fine]];

When given the original equation and one of the ones from my textbook it displays:

Column[func /@ {Function[{x, y}, (E^x + y)/(E^y + x)], Function[{x, y}, y (y - 3)]}]

Graph of m=(y+e^x)/(x+e^y) Graph of m=(y*(y-3))

Which gets very close to what I see my textbook. The range and options can be adjusted as needed.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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