0% found this document useful (0 votes)
45 views5 pages

ME 163 Direction Fields: Dirfield - NB

This document introduces a Mathematica function called "dirfield" that can be used to plot direction fields for first-order differential equations. It demonstrates using dirfield to plot direction fields for sample equations, including dy/dx = 2x + y, dy/dx = y^3 - x^3, and the logistic equation dP/dt = kP(1-P/Pm). Even without solving the equations, the direction fields provide insight into the qualitative behavior of the solutions.

Uploaded by

Naveen Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

ME 163 Direction Fields: Dirfield - NB

This document introduces a Mathematica function called "dirfield" that can be used to plot direction fields for first-order differential equations. It demonstrates using dirfield to plot direction fields for sample equations, including dy/dx = 2x + y, dy/dx = y^3 - x^3, and the logistic equation dP/dt = kP(1-P/Pm). Even without solving the equations, the direction fields provide insight into the qualitative behavior of the solutions.

Uploaded by

Naveen Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

dirfield.

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.

We consider first order differential equations of the form

ÅÅÅÅÅÅÅÅÅ = 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.

PlotVectorField@81, slope<, 8ind, indrange@@1DD, indrange@@2DD<,


In[16]:= dirfield@ind_, dep_, slope_, indrange_, deprange_, asprat_D :=

8dep, deprange@@1DD, deprange@@2DD<, Prolog -> [email protected]<, Axes -> True,


ScaleFunction -> H1 &L, AspectRatio -> asprat, PlotPoints -> 15, AxesLabel -> 8ind, dep<D

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

In[17]:= dirgraph = dirfield@x, y, 2 * x + y, 8-6, 6<, 8-6, 6<, 1D;

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

Now we construct two solutions, for x0 = -2, and 0.

In[19]:= sol1 = sol@-2D

Out[19]= -2 - 2 ‰2+x - 2 x

In[20]:= sol2 = sol@0D

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

In[21]:= solgraph = Plot@8sol1, sol2<, 8x, -6, 6<,


PlotRange -> 8-6, 6<, AspectRatio -> 1, PlotStyle -> RGBColor@1, 0, 0DD;

-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.

In[23]:= dirfield@x, y, y3 - x3 , 8-2, 2<, 8-2, 2<, 1D;

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:

In[24]:= DSolve@y '@xD == y@xD^ 3 - x ^ 3, y@xD, xD

Out[24]= DSolve@y£ @xD == -x3 + y@xD3 , y@xD, xD

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.

For our final example, we revisit the logistic equation

dPÅÅÅ = kP(1 - ÅÅÅÅÅÅÅÅ


ÅÅÅÅÅÅÅÅ PÅÅÅ ) .
dt PM
Here P is the population, k the natural growth rate, and PM the maximum sustainable population. We use the values that we
obtained in fitting the US Census Data up to 1920 to the model:

In[25]:= k = 0.03; Pm = 225.0;


dirfield.nb 5

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.

In[27]:= dirfield@t, P, k P H1 - P ê PmL, 80, 150<, 80, 300<, 0.7D

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.

You might also like