How can I plot the direction field for a differential equation?

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:

$$y'=\dfrac{y+e^x}{x+e^y}$$

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.

-

4 Answers

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

-

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

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

-

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

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

-

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

or perhaps you could use a stream plot:

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

-
lang-mma
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9540092349052429, "perplexity_flag": "head"}