Demo #3
Slope Fields with Mathematica

Consider the following nonlinear first-order ODE:

(dy/dx)=((y cos(x))/(1 + 2  (y^2)))

Let's examine the possible types of solutions that it can possess.  Our first step is to write the equation in standard form, i.e. y'(x) = f(x,y), by defining the function:

f[x_ ,y_ ]:= y Cos[x]/( 1 + 2 y^2 )

Next, we make sure that the proper graphics package has been loaded into Mathematica. (You should examine all the packages available under Add-ons in the Help menu):

<<Graphics`PlotField`

The slope field is drawn by the command:

PlotVectorField[ {1,f[x,y]}, {x,-3,3},{y,-1,2}]

I would like to have this plot better annotated, so that I know what the coordinates are.  I can do this by adding some commands to the PlotVectorField function:

plt1 = 
      PlotVectorField[{1, f[x, y]}, {x, -3, 3}, {y, -1, 2}, 
        Frame -> True, Axes -> True, 
        AxesLabel -> {"x", "y"}]

The plot came out smaller but at least now I know where the axes are.

Notice that I have named the plot "plt1" for later reference. The possible solutions to the ODE must "thread" through this slope field so that the solution is everywhere tangent to the field.

Next, we will generate an integral curve that represents the specific solution of an initial value problem. Suppose we are interested in the "particular solution" that satisfies the initial condition y[0]=1. It turns out that the equation is "separable", so that an (implicit) analytical solution is available. To save time, however, lets generate it numerically:

Clear[y]

NDSolve[ {y'[x] == f[x, y[x]], y[0]==1},y,{x,-3,3}]

plt2=Plot[Evaluate[y[x] /. %], {x,-3,3}]

Next, we superimpose this "integral curve" on top of the slope field, demonstrating explicitly how the solution is everywhere tangent to the slope field:

Show[plt1,plt2]

In summary, we see that the slope field is quite useful for visualizing families of solutions to first order ODE's and understanding qualitative solution behavior.

This problem can also be solved analytically by separation of variables.  Mathematica can help in this task too.  To this end, we rewrite the differential equation as:

	(dy/dx) = ((y cos x)/(1 + 2  (y^2)))
	
A little algebra separates the variables as follows:

	((1 + 2  (y^2))/y) dy  =  cos x dx

We now integrate both sides.  The right hand side is easy; for the left hand side we call on Mathematica

∫((1 + 2  (y^2))/y) \[DifferentialD]y

Thus after integrating both sides we have:

	(y^2) + ln y  =  sin x  +  C
	
where C is an integration constant.  We can determine the integration constant by plugging in the initial condition y(0) = 1.

(1^2) + Log[1] - Sin[0]

Thus, our particular solution is

	(y^2) + ln y  =  
        1 +  sin x