\magnification = \magstep1
\overfullrule=0pt
\centerline{Mathematics 136 -- Calculus 2}
\centerline{Lab Day 2:  slope fields for Differential Equations and Euler's Method}
\centerline{April 2, 2014}
\bigskip
\noindent
{\it Background}
\bigskip
To study first order differential equations
$${dy\over dx} = f(x,y)$$
and their solutions qualitatively, we can use the graphical {\it slope field\/} 
(or {\it direction field}, as in Stewart's section 7.2).
associated to the equation.  Recall, the slope field is a picture obtained
by placing at each point $(x_0,y_0)$ in the plane a small line segment
of slope equal to the number $f(x_0,y_0)$.  As we will see, 
Maple draws these as {\it arrows\/}.  A solution of the 
differential equation is then a function $y = g(x)$ whose graph has
slope equal to $f(x,g(x))$ at each point $(x,g(x))$ on the graph.  That is,
the graph {\it ``follows the slope field'' so that at each
point, its tangent line has the same slope as the slope field 
arrow at that point\/}.

Today, we will draw some additional slope fields, use them to visualize
solutions, and see how Euler's Method applies to give approximate solutions.  
\bigskip
\noindent
{\it 1. Slope Fields in Maple}
\bigskip
Begin by entering
\medskip
\centerline{\tt with(DEtools);}
\centerline{\tt with(plots);}
\medskip
\noindent
once at the start of the session.
Note the capitalization, which is necessary; DE stands for 
{\it D}ifferential {\it E}quation, as you might guess.  
This command loads an external package of additional Maple commands for
working with differential equations.  You need to do that to make the 
{\tt DEplot} command available -- the main 
operation we will be using today. The output will be a list of the
commands in the DEtools package.

To plot the slope field for an equation
$${dy \over dx} = f(x,y)$$
you can enter a command of the following format:
\medskip
\centerline{\tt DEplot(diff(y(x),x)=f(x,y(x)),y(x),x=a..b,y=c..d);}
\medskip
\noindent
where:
\item{$\bullet$} the {\tt f} is the expression for the slope 
function $f(x,y)$.  You enter that in usual 
Maple format (or use a symbolic name for the expression).
\item{$\bullet$}  The dependent variable (unknown function) in the 
differential equation should be entered in the form {\tt y(x)} everywhere.
For example, to plot the slope field for the equation 
${dy\over dx} = {7x-y\over x+2y}$, you 
can enter a command like
\medskip
\centerline{\tt DEplot(diff(y(x),x) = (7*x-y(x))/(x+2*y(x)),y(x),x=-2..2,y=-2..2);}
\item{$\bullet$} the ranges of $x$- and $y$-values indicate the portion of the 
plane that will be {\it plotted\/} with the slope field.  A standard 
number of grid points is used.

\bigskip
\noindent
{\it Lab Questions, Part I}
\bigskip
\noindent
For each of the following of the following differential equations,
generate a plot of the slope field on the given region of the plane.  
By eye, analyze the behavior of 
the solution passing through the given points $(x_0,y_0)$ -- that is the 
solution of the differential equation and the initial condition $y(x_0) = y_0$. 
What do think the graph does as $x \to +\infty$?
\bigskip
\item{A)}  
\itemitem{} Differential equation:  ${dy\over dx} = -4 x y$, 
\itemitem{} Region: $-3 \le x\le 3, 0 \le y \le 10$,
\itemitem{} Initial point: $(0,8)$ 
\item{B)}  
\itemitem{} Differential equation: ${dy \over dx} = y(y-1)(y-2)$, 
\itemitem{} Region: $-3 \le x \le 3, -1\le y \le 3$, 
\itemitem{} Initial points: $(0,0),(0,0.5),(0,1.5),(0,2.01)$.
\item{C)}  
\itemitem{} Differential equation: ${dy \over dx} = {\rm exp}(-x^2 + y)$,  
\itemitem{} Region: $-3\le x\le 3, -10 \le y \le 10$,
\itemitem{} Initial point: $(-3,-1)$.

\bigskip
\noindent
{\it Plotting Solutions in Maple}
\bigskip
To plot solutions of a differential equation, together with the
slope field, we can use the same {\tt DEplot} command above, but
with different options.  For example, 
to plot the solution of the equation ${dy\over dx} = -4 x y$ with $y(0)=8$, 
for $0 \le x \le 1$, you 
would enter a command like
\medskip
\centerline{\tt DEplot(diff(y(x),x) = -4*x*y(x),y(x),x=0..1,[[y(0)=8]],linecolor=black);}
\medskip
\noindent
If you want to see several solution curves, you can place a {\it list} of 
initial conditions, separated by commas,
in the $[ ]$ brackets after the $x = 0..1$.
For instance to see solution curves with $y(0) = .5$ and $y = 1$ on
the same axes, change to $[[y(0)=.5],[y(0)=1]]$.
Note: The default color for the solution curve is a shade of gold that does
not show up very well when you print a worksheet on a black and white
printer(!).  Hence we use the option {\tt linecolor=black}.
The general format is:
\medskip
\item{$\bullet$}  the differential equation will always come first, 
\item{$\bullet$}  then the dependent variable or unknown function ({\tt y(x)})  
\item{$\bullet$}  then {\it the range of $x$-values for which you want to see
the graph of the solution\/}.  
\item{$\bullet$}  then, in square brackets, separated by commas, a list
of initial conditions {\tt [y(a)=b]}.  In the example above, there's just one, 
but any number can be included.  
\bigskip
\noindent
{\it Lab Questions, Part II}
\bigskip
\noindent
\item{D)}  For each of the differential equations in questions I A,B,C above,
generate a single plot showing the slope field and the solution(s)
starting at the given point(s).  Did those solutions look like what
you expected?
\item{E)}  For equation C, experiment with different initial conditions
$y(-3) = y_0$.  What is the ``cut-off'' value for $y_0$ between solutions
that tend to a finite value as $t\to \infty$ and those that
grow without any bound?

\bigskip
\noindent
{\it Lab Questions, Part III}
\bigskip
In case you are wondering, Maple draws these graphs in a particular
way, by using an approximate numerical method similar to numerical
integration methods.  We will see a simplified version of this
called Euler's Method next.  The idea is this.  Suppose we want
approximate values for a solution of ${dy\over dx} = f(x,y)$ and $y(a) = y_0$
for $x$ in some interval $[a,b]$.  We can subdivide the interval as before
into $n$ equal subintervals with $\Delta x = {b - a\over n}$, proceed as follows:
\medskip
\item{1)}  ``sample'' the slope function at $(x_0,y_0)$ to get $f(x_0,y_0)$
\item{2)}  proceed along a straight line with slope $f(x_0,y_0)$ until 
$x = x_1 = x_0 + \Delta x$.  The $y$-coordinate on the line is 
one approximation for $y(x_1)$.  That can be found like this.  The line is 
$y - y_0 = f(x_0,y_0)(x - x_0)$ so when $x = x_1 = x_0 + \Delta x$, we get
$y_1 = y_0 + f(x_0,y_0)\Delta x$.  
\item{3)}  Then repeat the process starting from $(x_1,y_1)$.  
\medskip
\noindent
This idea yields the following recipe (Euler's Method) for computing 
the approximate $y$-values:  $y_0$ is given from the initial condition, then
$$y_k = y_{k-1} + f(x_{k-1},y_{k-1})\Delta x$$
for $k = 1, 2, \ldots, n$.  

In this part of the lab, you will use Maple to carry out 
the calculations for Euler's Method on the equation from Lab Question I A
and plot the results.  
\bigskip
\item{A)}  The following Maple commands will use Euler's Methods to generate
a table of approximate values of the solution of $y' = -4 x y$ with $y(0) = 8$ on
$[0,1]$ with $n = 4$, so $\Delta x = .25$.
\medskip
\leftline{$\qquad\qquad$ \tt xl[0]:=0: yl[0]:=8:}
\leftline{$\qquad\qquad$ \tt for i to 4 do}
\leftline{$\qquad\qquad\qquad$ \tt xl[i]:=xl[i-1]+.25;} 
\leftline{$\qquad\qquad\qquad$ \tt yl[i]:= yl[i-1]-4*xl[i-1]*yl[i-1]*(.25);} 
\leftline{$\qquad\qquad$ \tt end do;}
\medskip
\item{}
(To enter these, type Shift/Enter at the end of each line to 
start a new line, but staying within the same execution group.)
Then the following commands will plot the slope field, the piecewise
linear approximate solution graph made by connecting the dots for the points
computed above
\medskip
\leftline{$\quad$ \tt DirField:=DEplot(diff(y(x),x)=-4*x*y(x),[y(x)],x=-.1..1,y=0..10):}
\leftline{$\quad$ \tt Pts:=plot([seq([xl[i],yl[i]],i=0..4)],style=point,symbol=circle,color=blue):}
\leftline{$\quad$ \tt Lines:=plot([seq([xl[i],yl[i]],i=0..4)],color=black):}
\leftline{$\quad$ \tt display(DirField,Pts,Lines);}
\medskip
\item{} Enter these and execute them to see the graph.  (Note:  the {\tt display}
command is in the {\it plots} package.  If you get that line spit back at you 
with no output, you will need to execute {\tt with(plots);} (again).)  
\item{B)}  Now, modify the lines above computing the $x$- and $y$-lists to compute
a similar table of values but with $n = 20$, not $n = 4$.  There are number of things
you will need to change -- be sure you find them all!
\item{C)}  If you look carefully at your plots from parts A and B here, you should
see that there are times when Euler is underestimating the value you would get by 
following the slope field exactly and other times when Euler overestimates.  
What is the pattern?  What property of the actual solution says that Euler underestimates?
What property of the actual solution says that Euler overestimates?

\bigskip
\noindent
NOTE: In all questions here, some care may be needed
to get reasonable graphs.  As you will see, some of these solutions grow 
extremely fast and you may get nonsense output or an error message when 
that happens.  One possible solution is to {\it put in a range of $y$-values 
after the initial conditions in the {\tt DEplot} command}
if you suspect that there's a problem.  The graphing terminates
when the solution curve leaves the window you defined.  Try a 
restriction like {\tt y = -10..10}.
\bigskip
\noindent
{\it Assignment}
\bigskip
Writeups due by email to {\it jlittle@holycross.edu} by 5:00pm on Monday, 
April 7.
\bye


