Math 331.1  •  Fall 2005

Visualizing ODEs: Slope Fields

Original by Frank Wattenberg.
Edited and amended by Murray Eisenberg.

The function slopeField

The initialization cell below defines a Mathematica procedure slopeField that draws slope fields for a differential equation of the form

                                                             (y^( ′))  =  f(t,  y) .

If you did not allow Mathematica to evaluate the following cell when you opened this notebook, then evaluate it now.

Off[General::spell1]\(slopeField[foo_, tLow_, tHigh_, yLow_, yHigh_, \ steps_:  8] := 
	
      Module[{slopes = {}, 
			ht = \(tHigh - tLow\)\/\(2.5\ steps\), 
          hy = \(yHigh - yLow\)\/\(2.5\ steps\), 
          Δt = \(tHigh - tLow\)\/N[steps], 
          Δy = \(yHigh - yLow\)\/N[steps], t, y, i, j, m}, 
        For[t\  = \ tLow; \ i\  = \ 0, i\  ≤ \ steps, \ \(++i\); \ 
          t\  = \ t\  + \ Δt, 
			\ \ 
          For[y = yLow; j = 0, j ≤ steps, \(++j\); 
            y = y + Δy, 
					m = foo[t, y]; 
				
            If[Abs[m\ ht]\  ≤ \ hy, \ kt\  = \ ht, \ 
              kt\  = \ hy\/Abs[m]]; 
            slopes\  = \ Join[{Point[{t, \ y}]}, \ slopes]; 
				
            If[NumberQ[m], \ 
              slopes\  = \ 
                Join[{Line[{{t\  - \ kt, \ y\  - \ m\ kt}, 
                        \ {t\  + \ kt, \ y\  + \ m\ kt}}]}, \ slopes]]

            			]
		]; 
		
        Show[Graphics[{PointSize[0.01], \ slopes}], \ 
          Axes\  -> \ Automatic, \ AspectRatio\  -> \ 1.0, \ 
          DisplayFunction\  -> \ $DisplayFunction, 
		
          TextStyle\  -> 
            \ {FontWeight\  -> \ "Bold", \ FontSize\  -> \ 12}]
          
	]\)On[General::spell1]

I changed Wattenberg's constants there!  (And I made one of them—the number of steps in each direction—an additional but optional argument.)  Notice the  Off[General::spell1]...On[General::spell1] pair of expressions to avoid annoying warning messages about similarly spelled names.

Using slopeField

The following cell shows how SlopeField can graph a slope field like the ones in the browser window.  Evaluate it now.  When you evaluate this cell it will generate warning messages because the right hand side of this differential equation is undefined when t = 0.

Clear[fcn]fcn[t_, y_] := (y/t)slopeField[fcn, -3, 3, -3, 3];

Notice that in Mathematica the function f(t,  y) on the right hand side of the differential equation

	((dy)/(d t)) = f(t,  y)

must be specified in a line of its own rather than as part of the SlopeField line.  In other words, 

           WRONG!    slopeField[-t/y, -1.0, 1.0, -1.0, 1.0]     WRONG!

will not work.  In addition, the first variable must be the independent variable and the second variable must be the dependent variable.  For example, the differential equation

	((dp)/(d t)) = f(t,  p)

should be entered as

                 fcn[t_, y_]  :=  y(1 - y)    NOT    fcn[y_, t_]  :=  y(1 - y)

The form of the  SlopeField  statement is

	slopeField[function, tLow, tHigh, yLow, yHigh]

where  tLow  and  tHigh  give the range for the variable t, and  yLow  and  yHigh  give the range for the variable y.

Examples

Example 1

Clear[f]f[t_,  y_]  :=  - (t/y)

slopeField[f,  -5,  5,  -5,  5];

Example 2

Clear[f]f[t_,  y_]  :=  
      (y  -  1) (y  -  2) (y  -  3)

slopeField[f,   0,  5,  0,  2,  20]

Example 3

\(Clear[f]; 
f[t_, \ y_]\  := \ t\^2\  - \ y\)slopeField[f,  -2,  2,  -2,  2,  20];

Example 4

Clear[f]f[t_,  y_]  :=  (t^2)  -  yslopeField[f,  0,  1,  0,  1,  20];