Direction Fields

Debra Woods

Breif Description:  This project is a demonstration to show the visual behavior of solutions of differential equations of the form dy/dx = f(x, y).

Direction Fields

This project is a demonstration to show visually the behavior of solutions of differential equations of the form
 dy/dx = f(x, y).

Off[General::"spell1"];

For this demonstration we will consider the possible solutions to a differential equation of the form dy/dx = f(x,y).  A useful technique in visualizing the solutions to differential equations of this form is to sketch the direction field for the equation.  The direction field is a plot of tangent line segments to the solution at various points in a plane.  The slope of the tangent lines is given directly by the differential equation, dy/dx.  The function and limits of the graph can be changed in the first cell, in order to show other possible fields.  Students are encouraged to try various functions.  The example used here is dy/dx = x - y

Clear[top, bot, right, left, f, x, y, h, m, c, delx, dely, p, q, dx]
      top = 5; bot = -1; right = 5; left = -1; inc =  .5; f[x_, y_] := x - y; h =  .3; delx[m_] := (h/(Sqrt[(m^2) + 1])); dely[dx_] := (Sqrt[(h^2) - (dx^2)]); p = 
      Table[Graphics[{RGBColor[0, 0, 1], 
            Line[{{x, y}, {x + delx[f[x, y]], 
                  y + Sign[f[x, y]] dely[delx[f[x, y]]]}}]}], {x, left, 
          right, inc}, {y, bot, top, inc}]; Show[p, Axes -> True, AspectRatio -> 1, 
      PlotRange -> {bot, top}, AxesLabel -> {"x", "y"}, 
      DisplayFunction -> $DisplayFunction];

ISOCLINES

The locus of points for which f(x,y) = c is called an isocline.  That is, isoclines go through the line segments of equal slope on the direction field.  For the equation of this demonstration, they are the lines for which x - y = c.

isos = 
      ContourPlot[f[x, y], {x, left, right}, {y, bot, top}, 
        ContourShading -> False, DefaultColor -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity]; Show[p, isos, Axes -> True, AspectRatio -> 1, 
      PlotRange -> {bot, top}, AxesLabel -> {"x", "y"}, 
      DisplayFunction -> $DisplayFunction];

Solutions

The following shows that the family of solutions follow the direction of the line segments in the direction field.

Clear[Derivative]s = DSolve[(∂_x) y[x] == f[x, y[x]], y[x], x]; so := 
      s[[1, 1, 2]] /. C[1] -> c; 
    sol[x_, c_] := so; csol1 = NSolve[y == so, c] /. {x -> left, y -> bot}; csol2 = NSolve[y == so, c] /. {x -> left, y -> top}; csol3 = NSolve[y == so, c] /. {x -> right, y -> bot}; 
    csol4 = NSolve[y == so, c] /. {x -> right, y -> top}; 
    possmaxlist = {csol1[[1, 1, 2]], 
        csol2[[1, 1, 2]], 
        csol3[[1, 1, 2]], 
        csol4[[1, 1, 2]]}; maxval = Max[possmaxlist]; minval = Min[possmaxlist]; num = 50; delnum = Round[((maxval - minval)/num)]; sols = 
      Table[Plot[sol[x, c], {x, left, right}, 
          PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity, AspectRatio -> 1, 
          PlotRange -> {bot, top}], {c, minval, maxval, delnum}]; Show[p, sols, PlotRange -> {top, bot}, 
      DisplayFunction -> $DisplayFunction, Axes -> True, 
      AxesLabel -> {"x", "y"}, AspectRatio -> 1];

Go back to the first cell and experiment with different equations.  Try 
dy/dx = y + sin x,   dy/dx = (x^2) + y,  make up your own!