First-Order Differential Equations and Slope Fields

slopefield[f_,x_,y_,xmin_,xmax_,ymin_,ymax_,initcond_:Automatic]:=Block[{xloc,yloc,floc,h,k,segs},If[initcond===Automatic,flag=1,flag=0];floc[xloc_,yloc_]=f/.{x->xloc,y->yloc};icloc=initcond/.y->u;h=((xmax-xmin)/16);k=((ymax-ymin)/16);segs=Table[Line[If[(Denominator[floc[xloc,yloc]]/.{xloc->xmin+i h,yloc->ymin+j k})≠0,{{xmin+(i-3/(10 √(1+((floc[xmin+i h,ymin+j k])^2)))) h,ymin+j k-(floc[xmin+i h,ymin+j k] 3 h)/(10 √(1+((floc[xmin+i h,ymin+j k])^2)))},{xmin+(i+3/(10 √(1+((floc[xmin+i h,ymin+j k])^2)))) h,ymin+j k+(floc[xmin+i h,ymin+j k] 3 h)/(10 √(1+((floc[xmin+i h,ymin+j k])^2)))}},{{xmin+i h,ymin+(j-(3/10)) k},{xmin+i h,ymin+(j+(3/10)) k}}]],{i,0,16},{j,1,16}];If[flag≠1,soln=Table[NDSolve[{u'[xloc]==floc[xloc,u[xloc]],icloc[[i]]},u[xloc],{xloc,xmin,xmax}][[1,1,2]],{i,1,Length[icloc]}];(g2=Plot[Evaluate[soln],{xloc,xmin,xmax},PlotStyle->{{Thickness[0.008`],RGBColor[0.500008`,0,0.500008`]}},DisplayFunction->Identity];)];g1=Graphics[{RGBColor[0,0,1],Thickness[0.005`],segs}];If[flag≠1,Show[{g1,g2},PlotRange->{{xmin,xmax},{ymin,ymax}},Axes->True,AxesLabel->{x,y},ImageSize->{72 7,72 5},AspectRatio->1,DisplayFunction->$DisplayFunction],Show[{g1},PlotRange->{{xmin,xmax},{ymin,ymax}},Axes->True,AxesLabel->{x,y},ImageSize->{72 7,72 5},AspectRatio->1,DisplayFunction->$DisplayFunction]]]

Introduction

OBJECTIVE:  Visualize the slope fields and solution curves for selected first-order differential equations.

This module contains a special command that plots slope fields and selected solution curves for first-order differential equations. You can use it to obtain solutions for related problems in the text. In addition, you can use the slope field command to study a wide variety of first-order differential equations and to analyze the long-term behavior of solutions.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I - Drawing Slope Fields and Solution Curves

The slopefield[f_, x_, y_, xmin_, xmax_, ymin_, ymax_, initconditions_(optional)] command in the next cell generates a slope field and draws selected solution curves for first-order differential equations of the form (dy/dx)=f(x,y). The arguments of the command are f (the right-hand side function), f(x,y), x (the independent variable), y (the unknown function), xmin, xmax, ymin, and ymax (the bounds on the slope field), and initconditions (an optional list of initial conditions). If the list of conditions is not included, only the slope field is drawn. Here's how it works for the differential equation (dy/dx)=((x )/y). First, we plot only the slope field.

Clear[x,y,f]
f=(x/y)
slopefield[f,x,y,-4,4,-4,4]

Now we include a list of initial conditions to obtain some solution curves. Each of the four initial conditions will usually give a different solution curve.

slopefield[f,x,y,-4,4,-4,4,{y[0]==2,y[1]==1,y[1]==-1,y[0]==-2}]

There is a good likelihood that you will get error messages for some initial conditions. For example, the solution curve that passes through the point (1,1/2) cannot be expressed as a function when y is a function of x. Here's what happens.

slopefield[x/y,x,y,-4,4,-4,4,{y[1]==1/2}]

When this happens, it is probably better to hand sketch some representative solution curves on the slope field.

You Try It: Part I

It's fun! Make up some of your own differential equations of the form (dy/dx)=f(x,y), and see what kind of patterns you can generate. Can you form a differential equation that will give solution curves that are ellipses? hyperbolas? Just try it by changing the function of x and y.

Clear[x,y,f]
f=(x/y)
slopefield[f,x,y,-4,4,-4,4]

Part II: Antiderivatives

If we consider differential equations of the form (dy/dx)=f(x), then the solutions for y as a function of x are simply the antiderivatives of f(x); that is, y=∫f(x)dx. Let's look at some slope fields and solution curves for differential equations of this kind.

First we consider (dy/dx)=x.

Clear[f,x,y]
f=x;
slopefield[f,x,y,-4,4,-4,4,{y[0]==-2,y[0]==0,y[0]==2}]

Notice that the slopes in the field above are constant along any vertical line. This shows graphically that the derivative of y with respect to x is not a function of y, as reflected in the differential equation (dy/dx)=x. Along any horizontal line, however, the slopes vary with x, and the pattern shown in the slope field above is consistent with the differential equation (dy/dx)=x. The slopes are negative when x is negative, positive when x is positive, and zero when x is zero.

Let's try a periodic function, (dy/dx)=cos x.

f=Cos[x];
slopefield[f,x,y,-Pi,Pi,-Pi,Pi,{y[0]==-2,y[0]==0,y[0]==2}]

You Try It: Part II

Use the slope field command to study some antiderivative problems of the form (dy/dx)=f(x) where you pick the function f(x) (in red) and the initial conditions (in red). Don't forget to use the double-equals (==) if you add initial conditions.

Clear[x,y,f]
f=Exp[x];
slopefield[f,x,y,-4,4,-4,4,{y[0]==-3,y[0]==0,y[0]==2}]

Part III: Autonomous Differential Equations

Differential equations of the form (dy/dx)= f(y) are called autonomous differential equations. Let's look at the autonomous differential equation (dy/dx)=y. Despite its simplicity, this is surely the most famous of all first-order, autonomous differential equations. Let's draw the slope field and some solution curves.

Clear[f,x,y]
f=y;
slopefield[f,x,y,-3,3,-8,8,{y[0]==-2,y[0]==0,y[0]==2}]

Note that in the slope fields for (dy/dx)=y, the slopes are constant along any horizontal line and vary linearly along any vertical line. This is the reverse of what we observed for the antiderivative problem of the form (dy/dx)=x.

The logistic equation is another interesting autonomous first-order differential equation. Let's draw a slope field and some solution curves for a specific one, say (dP/dt)=P(1-P). The slopefield command will generate some error messages because two of the solution curves go off to infinity and have vertical asymptotes. However, the solution curves as drawn are okay.

Clear[f,t,P]
f=P (1-P);
slopefield[f,t,P,-5,5,-1,2,{P[0]==-0.005,P[0]==0.,P[0]==0.25,P[0]==0.5,P[0]==0.75,P[0]==1.0,P[0]==1.005}]

The long-term behavior of the solution to the logistic equation depends upon the initial condition. The slope field graph shown above suggests the following:  (Underscript[lim , t->∞])P(t)=-∞ when P(0)<0; P(t)=0 for all values of t when P(0)=0; and,  (Underscript[lim , t->∞])P(t)=1 when P(0)>0.

You Try It: Part III

The logistic equation has another interesting autonomous form. Let's draw a slope field and some solution curves for 

(dP/dt)=-P(1-P)(2-P). 

The slopefield command will generate some error messages. See if you can determine why this is so after you see the plot. The solution curves are drawn.

Clear[f,t,P]
f=-0.1*P (1-P)(2-P);
slopefield[f,t,P,-5,20,-2,5,{P[0]==-0.25,P[0]==-0.005,P[0]==.3,P[0]==0..7,P[0]==0.5,P[0]==1.3,P[0]==1.7,P[0]==2.3}]

Some solutions approach 2 and some approach 0. What is the dividing line between these sets of solutions? What has caused this to happen?

Look up the concepts of a threshold population size and a carrying capacity. See how those concepts apply here.

You Try It: General

Consider some nonautonomous differential equations of the form (dy/dx)=f(x,y). In this section we will start with the slopefield[ ] command to plot the slope fields, and we will ask you to sketch some representative solution curves. Here are a few examples. See if you can predict what the solution curves will look like by looking at the direction fields.  Describe the long-term behavior of the solutions for all possible initial values of y(0).

Solve  (dy/dx)=-(x/y)

f=-x/y;
slopefield[f,x,y,-4,4,-4,4]

Change the terms in red to whatever you wish. Remember to use the double-equal sign if you add more initial states. You can expect to get error messages with this. Why might you predict that?

slopefield[f,x,y,-4,4,-4,4,{y[0]==-1,y[0]==3}]

Solve  (dy/dx)=-(y/x)

f=-y/x;
slopefield[f,x,y,-4,4,-4,4]

Change the terms in red to whatever you wish. Remember to use the double-equal sign if you add more initial states. You can expect to get error messages with this. Why might you predict that? Why would you avoid initial conditions at x=0?

slopefield[f,x,y,-4,4,-4,4,{y[1]==-1,y[-1]==3}]

Solve  (dy/dx)=x y

f=x*y;
slopefield[f,x,y,-4,4,-4,4]

Change the terms in red to whatever you wish. Remember to use the double-equal sign if you add more initial states.

slopefield[f,x,y,-4,4,-4,4,{y[0]==-1,y[0]==3}]

Solve (dy/dx)=y-x

f=y-x;
slopefield[f,x,y,-4,4,-4,4]

Change the terms in red to whatever you wish. Remember to use the double equal sign if you add more initial states. Can you find a solution that is a straight line?

slopefield[f,x,y,-4,4,-4,4,{y[0]==-1,y[0]==3}]

Try More of Your Own

It's fun! Make up some of your own differential equations of the form (dy/dx)=f(x,y), and see what kind of patterns and solutions you can generate?