Slope fields in Mathematica
Math 330 
David McClendon

IMPORTANT:  Before you get started :  this file contains several commands of use.  Rather than editing these programs in this file, I suggest you open a new Mathematica file (File -> New -> Notebook), and copy and paste the cells containing these commands into your new file before editing/using them.  Recall that to highlight a cell, click on the "]" to the right of the cell (the cell's contents can then be copied using [CONTROL]+C, etc.)

Command 1 (Plotting a Slope Field) :

In lecture we discussed how to associate to any first-order ODE of the form y' = ϕ(t, y) a slope field.  Although we were able to work out slope fields in a couple of specific examples, in general it is quite difficult to get a good picture of a slope field by hand.  Fortunately Mathematica sketches pictures of slope fields with ease.  Here is a command which plots the slope field associated to the differential equation y' = 3y, which we sketched in class during lecture:

phi[t_,y_] := 3y; 
VectorPlot[{1,phi[t,y]},{t,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange, VectorColorFunction -> None]

Here is what this command actually did.  First, the function ϕ(t, y) is defined in the first line.  If you wanted to sketch a slope field for a different first-order ODE, you can change the "3y" here to whatever formula is given by ϕ(t, y).  Second, the VectorPlot command tells Mathematica to sketch the slope field.  The relevant ingredients here are as follows :
   - The "{t, -3, 3}" and "{y, -3, 3}" tell Mathematica to produce a picture that runs from t = -3 to t = 3 and y = -3 to y = 3, i.e. it specifies the range of the picture.
  - The "VectorPoints -> 20" tells Mathematica how many vectors to draw.  If you change 20 to a larger number, you get more densely drawn arrows.   This produces a more accurate picture of the vector field, but the program will take longer to run.  On the other hand, if you change 20 to a smaller (positive) number, less arrows will be drawn, resulting in a less dense picture.
  - The "Axes -> True" tells Mathematica to draw the t- and y- axes on the picture.
  - The "VectorScale -> {Automatic, Automatic, None}" tells Mathematica to make sure all the vectors are scaled so that you can see them (without this command many of the vectors drawn would be too small to see).
  - The "VectorStyle -> Orange" tells Mathematica to make the vectors orange.  An interesting thing to do is to replace this command with "VectorColorFunction -> Hue" to get a 'tie-dye' looking picture.

Command 2 (Graphing a family of solution curves with the slope field) :

Here is a command which plots both the slope field and some solution curves to the differential equation y' = ϕ(t, y).  It comes from taking Command 1 above and adding some additional programming inside the VectorPlot command.  Execute it and see what you get :

phi[t_,y_] := 3y; 
VectorPlot[{1,phi[t,y]},{t,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange, VectorColorFunction -> None,StreamPoints ->35, StreamScale -> Full, StreamStyle -> {Blue, Thick}, StreamColorFunction -> None]

As before, the first line defines ϕ(t, y); the "{t, -3, 3}" and "{y, -3, 3}" determine the range of the picture; the "VectorPoints -> 20" tell Mathematica how many arrows to draw; "Axes -> True" tells Mathematica to draw the t - and y - axes; and the "VectorScale..." command ensures the vectors are big enough to see.
  What' s new here are the commands regarding "Streams".  The "StreamPoints -> 35" asks Mathematica to draw 35 stream lines (a stream line is a graph that ' follows' the vector field as described in lecture).  If you lower the number 35, less stream lines are drawn and if you increase the number, more stream lines are drawn.
  The "StreamScale-> Full" command ensures that the stream lines are connected when they should be (so that you get a much better picture).  The last bit tells Mathematica what color to make the stream lines and to make them thick.

Command 3 (Graphing a solution curve through a specified point) :

Suppose you wanted to draw one stream line that went through one specified point.  In this case, you modify the above command by changing the StreamPoints directive from 35 to "{{a,b}}" where you want to draw the stream line through the point (a, b).  For example, in the above picture, if you wanted the stream line passing through the point (1, 2), your command would be :

phi[t_,y_] := 3y; 
VectorPlot[{1,phi[t,y]},{t,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange, VectorColorFunction -> None, StreamPoints ->{{1,2}}, StreamScale -> Full, StreamStyle -> {Blue, Thick}, StreamColorFunction -> None]