Direction Fields

Math 151-03 (Pelsmajer)
Adam Rumpf
April 3, 2014

Directions:
Read through this workbook, evaluating the Mathematica code as you go.  Do the exercises.  Ask questions as needed!
As before, you will need to write up your investigations in the form of a lab report. 

Tip: To make sure you get all the little details right (many parentheses, double-equals, capitalization), copy-and-paste from working examples and then carefully make changes to suit your present needs.

I. Direction Fields
If we are given a differential equation

                                               (dy/dx)  =  F(x, y) ,
then  F(x, y)  is the slope of a solution curve as it passes through the point (x,y) . 

If we draw a short directed line segment having slope  F(x, y)  at a large number of points ( x , y ) ,  we get a direction field (or slope field).
A direction field is used to visualize the general shapes of solution curves.  Direction fields can be generated using  Mathematica. 
  
  We obtain this field using the command  VectorPlot. VectorPlot is meant to visualize velocity fields.
  It takes three agruments:
	• The first argument is a list showing the velocities (or rates of change) in the x and y directions, 
	   respectively. For a differential equation of the form  (dy/dx)=f(x,y),  this will always be 
	  {1,f[x,y]}  since (dx/dx)=1.
  	• The second and third arguments are the range over which to plot. Just like with the Plot command, 
  	   these are of the form {variable, lowerbound, upperbound}.
  	   
  Consider the differential equation:
  
                                  (dy/dx)  = (e^((-x)))-2y                  ( 1 ) 
                                               
We first define the derivative, (dy/dx) , in  (1)  as a function of two variables and then plot its slope field.

Clear[x,y]
f[x_, y_]:= Exp[-x]-2*y 
VectorPlot[{1,f[x,y]},{x,-4,4},{y,-4,4},Axes->True]

By default, the lengths of the arrows are dependent of the relative magnitudes of the slopes. This can make the plot very difficult to read since some arrows are very small.

In order to change the relative sizes of the arrows, we use the VectorScale option. VectorScale is passed as a list: the first item in the list is the thickness of the arrow as a whole, the second is the thickness of the head only, and the third is how they scale with magnitude. Useful options are Tiny, Small, Medium, Large, and Automatic for the first two, (decimal values may also be used in these positions) and None and Automatic for the last.

HeyItsADirectionField=VectorPlot[{1,f[x,y]},{x,-4,4},{y,-4,4},VectorScale-> {Tiny,Small,None},VectorStyle->Lighter[Gray],Axes->True]

We may even ask  Mathematica  to plot the solution curves passing through twenty randomly selected initial points by adding the option “StreamPoints→20” to the VectorPlot instruction:

VectorPlot[{1,f[x,y]},{x,-4,4},{y,-4,4},VectorStyle->Lighter[Gray],VectorScale-> {Tiny,Tiny,None},Axes->True,StreamPoints-> 20]

Allternatively, we may specify the particular set of initial points that the solution curves should pass through.

VectorPlot[{1,f[x,y]},{x,-4,4},{y,-4,4},VectorScale-> {Tiny,Tiny,None},Axes->True,AxesStyle->Black,VectorStyle->Lighter[Gray],StreamPoints-> {{-3,2},{-2,2},{0,-2}}]

The additional option, StreamStyle,can be used to further control the look of the solution curves:

VectorPlot[{1,f[x,y]},{x,-4,4},{y,-4,4},VectorScale-> {Tiny,Small,None},Axes->True,AxesStyle->Black,VectorStyle->Lighter[Gray],StreamPoints-> {{-3,2},{-2,2},{0,-2}},StreamStyle->{Darker[Blue],Thickness[0.005]}]

II. Solving Differential Equations with Mathematica

We may use Mathematica to find both the general solution to a differential equation as well as the exact solution to an initial value problem.  To solve the earlier differential equation, we enter the following instructions:

Clear[x,y]
DSolve[y'[x]==Exp[-x]-2*y[x],y[x],x]

Observe that C[1] denotes the arbitrary constant parameter in the Mathematica solution.
To solve the intial value problem where we want the solution through the point (x,y)=(-2,-2):

Clear[x,y]
DSolve[{y'[x]==Exp[-x]-2*y[x],y[-2]==1},y[x],x]

Now let's give a name to the solution of the IVP and plot its graph.

Y[x_]:=(E^(-4-2 x)) (1-(E^2)+(E^(4+x)))
ExactSol=Plot[Y[x],{x,-2,4},PlotStyle->{Red,Thickness[0.004]}]

Finally, we apply the Show instruction to merge the plots of the the exact solution to the IVP together the direction field for the associated DE:

Show[{HeyItsADirectionField,ExactSol},AspectRatio->Automatic]

Exercises

1.  A direction field and several solution curves for the differential equation: 
  
                                  (dy/dx)  +  y  =  1 + sin (4x) - 2 cos (6x)   
      
       are displayed in the figure below.

(a)  Based on this figure explain in words what appears to happen to each of the solution curves
               as x becomes very large?

(b)  Use  Mathematica  to solve this differential equation. Then explain how the general solution  
               supports your observation in part (a) .

2.  Perform the following steps in regards to the differential equation:

                                (dy/dx)  = 4x - (( 2y)/x) .         
                                
     (a)  Plot a direction field for the DE over the region:  - 2  ⩽  x  ⩽  2 ,  - 4 ⩽  y  ⩽ 8 .

(b)  Add twenty solution curves to the slope field from part (a) using the StreamPoints -> 20  option .

(c)  Use  Mathematica  to solve the differential equation .

(d) Use Mathematica  to find the particular solution to the DE which passes through the point  (x,y) = ((1/2) , 3) .   Also, plot the solution over the x-interval   [ (1/2) , 2] .

(e)   Use “Show” to combine the previous two plots, showing the Direction Field for the general solution as well as the specific solution to the initial-value problem.  (And check that the specific solution from part (d) has the appropriate shape.)