Vector Fields and Stream Plots in Mathematica
Math 220 
David McClendon

IMPORTANT:  Before you get started :  this file contains several programs 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.)

In lecture we discussed how to associate to any first - order differential equation y' = g (x, y) a vector field.  Although we were able to work out vector fields in a couple of specific examples, in general getting a good picture of a vector field is quite difficult.  Fortunately Mathematica sketches pictures of vector fields with ease.  Here is a command which plots the vector field associated to the differential equation y' = 3y, which we sketched in class during lecture.  Execute it and see what happens:

Command 1 (Plotting a Vector Field) :

g[x_,y_] := 3y; 
VectorPlot[{1,g[x,y]},{x,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange]

Let me explain what this command actually did.  First, the function g (x, y) is defined in the first line.  If you wanted to sketch a vector field for a different equation, you can change the "3y" here to whatever formula is given by g (x, y).  Second, the VectorPlot command tells Mathematica to sketch the vector field.  The relevant ingredients here are as follows :
   - The "{x, -3, 3}" and "{y, -3, 3}" tell Mathematica to produce a picture that runs from x = -3 to x = 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 x- 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 (Producing Stream Plots) :

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

g[x_,y_] := 3y; 
VectorPlot[{1,g[x,y]},{x,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange, StreamPoints ->35, StreamScale -> Full, StreamStyle -> {Blue, Thick}]

As before, the first line defines g (x, y); the "{x, -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 x - 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.
  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 :

Command 3 (Producing stream plots through a specified point) :

g[x_,y_] := 3y; 
VectorPlot[{1,g[x,y]},{x,-3,3},{y,-3,3}, VectorPoints -> 20, Axes -> True, VectorScale->{Automatic,Automatic,None}, VectorStyle -> Orange, StreamPoints ->{{1,2}}, StreamScale -> Full, StreamStyle -> {Blue, Thick}]

From lecture we described a numerical method of approximating solutions to first-order differential equations of the form y' = g(x,y) called Euler's method.  To implement Euler's method, first execute the following command once (this command will need to be executed once every time you restart Mathematica before you can apply it):

Command 4 (Defining code for Euler's method) :

euler[f_,{x_,x0_,xn_},{y_,y0_},steps_]:=Block[{xold=x0,yold=y0,thelist={{x0,y0}},x,y,h},h=N[(xn-x0)/steps];
Do[xnew=xold+h;
ynew=yold+h*(f/.{x->xold,y->yold});
thelist=Append[thelist,{xnew,ynew}];
xold=xnew;
yold=ynew,{steps}];
Return[thelist];]

This command, in some sense, created a "black box" called euler which can henceforth be used to produce output obtained by Euler' s method.  After the above command is run once, you can then type in and execute commands like this (don' t execute this yet) :

Command 5 (Executing Euler's method) :

euler[3y, {x, 1, 3}, {y, -1}, 400]

What this does is performs Euler's method for the differential equation y' = 3y where the initial point is ((Subscript[x, 0]), (Subscript[y, 0])) = (1,-1), the procedure stops when x = 3, and there are 400 steps used.  This command will produce a (very long) list of points starting with (1,-1), (1.005, -1.015), ..., and ending with (3, -385.849).   This is the same list of points ((Subscript[x, n]), (Subscript[y, n])) that you would produce via Euler's method by hand.  If you don't want to see this long list, put a semicolon (;) at the end of the command and then execute it; this performs the operation without displaying the output.  Or you can just delete the list.

Command 6 (Sketching the points obtained from Euler's method) :

If you only care about the graph you'd get from Euler's method, you can use the following command.  It simply plots all the points ((Subscript[x, n]), (Subscript[y, n]))obtained by running Euler's method using the same parameters as above.  Since there are so many points, it appears as though they make a curve (since the points are really close together).

ListPlot[euler[3y, {x, 1, 3}, {y, -1}, 400]]

Again, in this command the starting point is (1,-1), you stop at x = 3, and there are 400 steps used in the computation so there are 400 (maybe 401) points plotted.