ME 163 Direction Fields: Dirfield - NB
ME 163 Direction Fields: Dirfield - NB
nb 1
ME 163
Direction Fields
In this notebook, we define Mathematica code for plotting direction fields for first order differential equations. As in
all programming, there is a trade-off between flexibility and simplicity. In this notebook, we opt in favor of simplicity. The
resulting code, although limited, will serve our purposes for now.
ÅÅÅÅÅÅÅÅÅ = F Hx, yL .
dy
dx
We wish to plot the direction field defined by the slope function F(x,y). Our first step is to load into Mathematica a special
graphics package called PlotField which is needed to construct the direction fields. This is accomplished by the command
below.
In[1]:= Needs@"Graphics`PlotField`"D
We are now going to define a function called dirfield which will construct a direction field plot. The function will
have six arguments: independent variable, dependent variable, slope, range of independent variable, range of dependent
variable, and aspect ratio of graph. The definition of the function is given below. You should not expect to understand the
definition in detail at this point, because you have not learned much Mathematica yet. What you will be able to do at this
point is to use this function to study direction fields.
We try this on Problem 2 of exercises 1.3 in the text. The differential equation in that problem is
dy
ÅÅÅÅÅÅÅÅÅ = 2 x + y .
dx
We will construct the direction field for an x-range of [-6,6], a y-range of [-6,6], and an aspect ratio of 1.
dirfield.nb 2
x
-6 -4 -2 2 4 6
-2
-4
-6
As you can see, the function is very easy to use, even though it's definition looks complicated. If you print out this
direction field, you can then sketch integral curves on it and get a good idea of what the solutions look like. Or we can
construct the integral curves with Mathematica. We do that here. We use DSolve, and the initial condition y[x0]==0, where
we will vary x0.
In[18]:= sol@x0_D := y@xD ê. Flatten@DSolve@8y '@xD == 2 x + y@xD, y@x0D == 0<, y@xD, xDD
Out[19]= -2 - 2 ‰2+x - 2 x
Out[20]= -2 + 2 ‰x - 2 x
We plot these in red and then combine the plot with the direction field plot.
dirfield.nb 3
-6 -4 -2 2 4 6
-2
-4
-6
In[22]:= Show@8dirgraph, solgraph<, PlotRange -> 88-6, 6<, 8-6, 6<<D;
y
6
x
-6 -4 -2 2 4 6
-2
-4
-6
dirfield.nb 4
We do another example. This equation comes from Problem 8 of Exercises 1.3 in the text.
dy
ÅÅÅÅÅÅÅÅÅ = y3 - x3 .
dx
We construct the direction field for the range [-2,2] in both x and y.
x
-2 -1 1 2
-1
-2
Here's what happens if we try to use DSolve to generate a solution to plot on the direction field:
Mathematica can't find the answer, so it simply echoes the input. The proper tool for solving this nonlinear equation is
NDSolve, a numerical integrator which we will learn about later. Even without the solutions, we can see from the direction
field what paths the solutions take.
Now we construct the direction field. We use a time interval of 150 years, a P range of {0, 300} (in millions), and an aspect
ratio of 0.7.
P
300
250
200
150
100
50
t
25 50 75 100 125 150
Out[27]= Ü Graphics Ü
Even without plotting any specific solutions, we can see the basic features of the system from the direction field. For
populations starting above Pm, a situation not discussed in class, it is clear that the population decreases and approaches Pm
from above. It is equally clear that populations starting below Pm approach it asymptotically from below without overshoot.
Thus the constant state solution P = Pm is the eventual fate of any initial condition. The value of the direction field is that it
provides such insights without requiring us to actually solve the equation.