\documentclass[12pt]{article}
\usepackage{latexsym,amsmath,epsfig,graphicx}
\newcommand{\ds}{\displaystyle}
\newcommand{\vp}{$\vphantom{\big|}$}

\newcommand{\R}{\textbf{R}}
\newcommand{\Item}{\item{$\Box$}\quad}

\newcommand{\Answer}[1]{$\phantom{.}$\hfill
        #1\quad\fboxsep 20pt \fbox{$\phantom{\Big|Answer}$}} 

\newcommand{\MyTallAnswer}[2]{$\phantom{.}$\hfill
        #1\quad\fboxsep 20pt\fbox{$\vphantom{\Big|}#2$}}

\newcommand{\MyAnswer}[2]{$\phantom{.}$\hfill
        #1\quad\fboxsep 5pt\fbox{$\vphantom{\Big|}#2$}}

\newcommand{\soln}{\paragraph{Solution}}
\newcommand{\sep}{{\vskip 10pt\hrule\vskip 10pt}}
\newcommand{\blank}{\underline{$\phantom{A blank}$}}

\setlength{\textwidth}{6.5in}
\setlength{\topmargin}{-0.5in}
\setlength{\oddsidemargin}{0in}
\setlength{\textheight}{9.0in}
\overfullrule=0pt
\begin{document}
\begin{center}
\centerline{Mathematics 136 -- Calculus 2}
\centerline{Slope fields for Differential Equations and Euler's Method}
\centerline{November 11, 2016}
\end{center}
\vskip 10pt
\noindent
{\it Background}
\vskip 10pt
To study a first order differential equation
$$\frac{dy}{dt} = f(t,y)$$
and its solutions qualitatively, we can use the graphical {\it slope field\/} 
associated to the equation.  The slope field is a picture obtained
by placing at each point $(t,y)$ in the plane a small line segment
of slope equal to the number $f(t,y)$.  As we will see in a moment, 
Maple draws these as {\it arrows\/}.  A solution of the 
differential equation is then a function $y = g(t)$ whose graph has
slope equal to $f(t,g(t))$ at each point $(t,g(t))$ on the graph.  That is,
the graph {\it ``is tangent to the slope field''\/} --  at each
point on the graph of the solution function, the tangent line has the same 
slope as the slope field arrow at that point.

Today, we will use Maple to draw some slope fields, use them to visualize
solutions, and see how numerical methods like Euler's Method apply to give 
approximate solutions.  We will use the logistic equations 
$$\frac{dy}{dt} = ky\left(1 - \frac{y}{A}\right)$$
as examples to prepare for work you will be doing in the computer lab next
week.  
\vskip 10pt
\noindent
{\it 1. Slope Fields Via Maple}
\vskip 10pt
Executing the commands
\vskip 10pt
\centerline{\tt with(DEtools);}
\centerline{\tt with(plots);}
\vskip 10pt
\noindent
once at the start of the session set up the plotting routines used for 
differential equations and slopefields.
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
$$\frac{dy}{dt} = f(t,y)$$
you can enter a command of the following format:
\vskip 10pt
\centerline{\tt DEplot(diff(y(t),t)=f(t,y(t)),y(t),t=a..b,y=c..d);}
\vskip 10pt
\noindent
where:
\begin{itemize}
\item The {\tt f} is the expression for the slope 
function $f(t,y)$.  You enter that in usual 
Maple format (or use a symbolic name for the expression).
\item The dependent variable (unknown function) in the 
differential equation should be entered in the form {\tt y(t)} everywhere.
For example, to plot the slope field for the logistic equation 
$\frac{dy}{dt} = (.6)y(1 - y/10)$, you 
might enter a command like
\vskip 10pt
\centerline{\tt DEplot(diff(y(t),t) = (0.6)*y(t)*(1 - y(t)/10),y(t),t=0..5,y=0..15);}
\item the ranges of $t$- 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.  The output is shown in Figure 1 above.
\end{itemize}

\begin{figure}
\begin{center}
\includegraphics[width=.4\linewidth]{SF.png}
\caption{Slope Field for $\frac{dy}{dt} = (.6)y(1-y/10)$}
\end{center}
\end{figure}

\vskip 10pt
\noindent
{\it Plotting Solutions in Maple}
\vskip 10pt
It's possible, of course to plot the graph of an analytic solution (i.e. a
formula derived by a method like separation of variables) of a 
differential equation directly using the {\tt plot} command.  But even
if we have not or can not do that, we can plot {\it approximate solution graphs}
for the solutions of initial value problems for a differential equation, 
together with the slope field with the same {\tt DEplot} command above, but
with different options.  For example, 
to plot the solution of the logistic equation above with $y(0)=1$, 
for $0 \le t \le 5$, you 
would enter a command like
\vskip 10pt
\centerline{\tt DEplot(diff(y(t),t) = (0.6)*y(t)*(1-y(t)/10),y(t),t=0..5,y=0..15,[[y(0)=1]],linecolor=black);}
\vskip 10pt
\noindent
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 I used the option {\tt linecolor=black}.
The general format is:
\vskip 10pt
\begin{itemize}
\item  The differential equation will always come first, 
\item  then the dependent variable or unknown function ({\tt y(t)}),
\item  then {\it the range of $t$-values for which you want to see
the graph of the solution\/}, 
\item  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.  
\end{itemize}
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 {\tt y = 0..15}.
This plot in Figure 2 shows several such solutions, corresponding to the initial conditions
$$y(0) = 1, y(0) = 5, y(0) = 13$$

\begin{figure}
\begin{center}
\includegraphics[width=.4\linewidth]{SFandSols.png}
\caption{Slope Field for $\frac{dy}{dt} = (0.6)y(1-y/10)$, together with three solution graphs}
\end{center}
\end{figure}

\vskip 10pt
\noindent
{\it How does Maple do this?}
\vskip 10pt
\vskip 10pt
\begin{figure}
\begin{center}
\includegraphics[width=.4\linewidth]{Euler.png}
\caption{Euler's Method on $\frac{dy}{dt} = -4 t y$ with initial condition $y(0) = 8$}
\end{center}
\end{figure}

In case you are wondering, Maple draws these graphs in a particular
way, by using an \emph{approximate numerical method similar to numerical
integration methods}.  There are many such methods; we will briefly discuss one
called \emph{Euler's Method} next.  The idea is this.  Suppose we want
approximate values for a solution of $\frac{dy}{dt} = f(t,y)$ and $y(a) = y_0$
for $t$ in some interval $[a,b]$.  We can subdivide the interval as before
into $n$ equal subintervals with $\Delta t = \frac{b - a}{n}$, proceed as follows:
\vskip 10pt
\begin{itemize}
\item  ``sample'' the slope function at $(t_0,y_0)$ to get $f(t_0,y_0)$
\item proceed along a straight line with slope $f(t_0,y_0)$ until 
$t = t_1 = t_0 + \Delta t$.  The $y$-coordinate on the line is 
one approximation for $y(t_1)$.  That can be found like this.  The line is 
$y - y_0 = f(t_0,y_0)(t - t_0)$ so when $t = t_1 = t_0 + \Delta t$, we get
$y_1 = y_0 + f(t_0,y_0)\Delta t$.  
\item  Then repeat the process starting from $(t_1,y_1)$. 
\item  Keep going until you have a table of values for the desired $t$'s, or 
for some predefined number of steps.  
\end{itemize} 
\vskip 10pt
\noindent
This idea yields the following recipe (called \emph{Euler's Method}) for computing 
the approximate $y$-values:  $y_0$ is given from the initial condition, then
$$t_k = t_{k-1} + \Delta t$$
and
$$y_k = y_{k-1} + f(t_{k-1},y_{k-1})\Delta t$$
for $k = 1, 2, \ldots, n$.  

\vskip 10pt
The following Maple commands will use Euler's Method to generate
a table of approximate values of the solution of $y' = -4 t y$ with $y(0) = 8$ on
$[0,1]$ with $n = 4$, so $\Delta x = .25$.
\vskip 10pt
\leftline{$\qquad\qquad$ \tt tl[0]:=0: yl[0]:=8:}
\leftline{$\qquad\qquad$ \tt for i to 4 do}
\leftline{$\qquad\qquad\qquad$ \tt tl[i]:=tl[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;}
\vskip 10pt

Then the following commands generate Figure 3 above: they plot the slope field together 
with the piecewise linear approximate solution graph made by ``connecting the dots'' for the points
computed by Euler's Method:
\vskip 10pt
\leftline{$\quad$ \tt DirField:=DEplot(diff(y(t),t)=-4*t*y(t),[y(t)],t=-.1..1,y=0..10):}
\leftline{$\quad$ \tt Pts := plot([seq([tl[i],yl[i]],i = 0..5)],color=blue,style=point,symbol=circle);}
\leftline{$\quad$ \tt Lines:=plot([seq([tl[i],yl[i]],i=0..4)],color=black):}
\leftline{$\quad$ \tt display(DirField,Pts,Lines);}


\end{document}

\bye


