
\input{PreambleCommon}
\input{../WeekTitles}
\begin{document}

\newpage
\setfont
\pagestyle{fancy}
\renewcommand{\Week}{7}
\renewcommand{\WeekTitle}{\WeekTitleSeven}

\fancyhead[LE,RO]{Week \Week}  % default, usually only for first page
\fancyfoot{}
\sectionbox{Week \#\Week: \WeekTitle}

\vspace{5mm}
\goals 
\begin{itemize}
\item Understand and express real world situations in terms of first order differential equations
\item Tell the difference between linear and nonlinear differential equations
\item Solve basic first order separable and linear differential equations
\item Use MATLAB to solve nonlinear first order differential equations
\end{itemize}

\topic{Sources of Differential Equations}
\section*{Differential Equations}  

An equation relating an unknown function and one or more of its
derivatives is a \textbf{differential equation}.  

We study differential equations because they are an incredibly
powerful tool for modelling real world problems.

%\newpage
%
%\begin{problem}Express the following sentence as an equation:\\[1ex]
%The rate at which water is leaking from the tank is
%      proportional to the square root of the volume of water in the
%      tank.
%\end{problem}

%\newpage
%
%\vfill
%\begin{problem}Express the following sentence as an equation: \\[1ex]The
%  rate at which a rumour spreads is proportional to the product of the
%  people who have heard it and those who have not.
%\end{problem}
%
%\vfill


%\newpage
%\begin{problem}Express the following sentence as an
%  equation:\\[1ex]
%As the meteorite plummets toward the Earth, its
%      acceleration is inversely proportional to the square of its
%      distance from the centre of the Earth.
%\end{problem}
%
%\vfill
%
%The previous examples indicate how easily differential equations can
%be constructed.  Unfortunately, starting with those equations, we have
%a lot of work to do before we can predict will happen, given the
%equation.

% \newpage
% \topic{The Exponential Differential Equation}
% We now consider the most fundamental differential equation.
% \begin{problem}
%   Consider 
%   \begin{equation}
%     \label{eq:one}
%     \tag{*}
%     \frac{dy}{dt} = k y \qquad \text{where k } \in \mathbb{R}
%   \end{equation}
%   What functions (if any) satisfy \eqref{eq:one} when $k = 1$?

% \newpage
% $$\eqref{eq:one} ~~~~~  \frac{dy}{dt} = k y \qquad \text{where } k \in \mathbb{R}$$
%   For what values of $k$ does a function $y$ exists that satisfies
%     \eqref{eq:one}?

% \newpage
% $$\eqref{eq:one} ~~~~~  \frac{dy}{dt} = k y \qquad \text{where } k \in \mathbb{R}$$
%   How many functions satisfy \eqref{eq:one} for each value of $k$?


% \newpage
% $$\eqref{eq:one} ~~~~~  \frac{dy}{dt} = k y \qquad \text{where } k \in \mathbb{R}$$
%   How many functions $y$ are there such that $y$ and $\dfrac{dy}{dt}$ are
%     continuous for all real $t$, $y(0) = 0$, and satisfy \eqref{eq:one}?  What
%     are they?

% \newpage
% $$\eqref{eq:one} ~~~~~  \frac{dy}{dt} = k y \qquad \text{where } k \in \mathbb{R}$$
%   How many functions $y$ are there such that $y$ and $\dfrac{dy}{dt}$ are
%     continuous for all real $t$, $\mathbf{ y(1) = 1}$, and satisfy \eqref{eq:one}?  What
%     are they?
% \end{problem}

% \newpage

%    First, we observe that $e^{kx}$ satisfies \eqref{eq:one}: $\dfrac{d}{dx}
%    \bigl( e^{kx}) \bigr) = k e^{kx}$.  Suppose $g(x)$ is another function that
%    satisfies \eqref{eq:one}.  Since $e^{kx}$ is never zero, we may write $g(x) =
%    e^{kx} h(x)$ for another function $h(x)$.  Hence, $g'(x) = \tfrac{d}{dx}
%    \bigl( e^{kx} h(x) \bigr) = k e^{kx} h(x) + e^{kx} h'(x) = k \, g(x)$ so
%    $h'(x) = 0$ and $h(x)$ is a constant.  Therefore, the only possible solution
%    to \eqref{eq:one} is a constant multiple of $e^{kx}$.
%    \begin{enumerate}
%    \item All functions of the form $A e^x$ where $A \in \mathbb{R}$.
%    \item There are solutions for every value of $k$.
%    \item There are infinitely many solutions for each value of $k$.
%    \item There is only one function with the required properties: $f(x) = 0$.
%    \item There is only one function with the required properties: $g(x) =
%      e^{k(x-1)} = e^{-k} e^{kx}$.
%    \item When $k = \ln(2)$, there is a unique function: $g(x) = 2^{x-1} =
%      e^{\ln(2) (x-1)}$; otherwise there is no function. 
%    \end{enumerate}

\newpage
\topic{Using MATLAB to Solve Differential Equations}
\section*{Using MATLAB to Solve Differential Equations}  
Since this course has a strong computational component, we will be using MATLAB to solve the majority of differential equations problems we come across. Be sure to watch the videos that accompany each problem to see how to enter code into MATLAB. This link also provides some help:\\
http://www.mathworks.com/help/matlab/ref/ode45.html\\
ode45 is going to be your new best friend when it comes to differential equations!



\newpage
\topic{General and Particular Solutions}
\begin{boxnote}
  The \textbf{solution} is the \emph{family} of all possible
  functions that satisfy a differential equation.  It normally
  involves some parameters,
  i.e.\ arbitrary constants.  \\[1ex]
  A \textbf{particular solution} is a specific function with satisfies
  the differential equation. We will be dealing with particular solutions in this course.
\end{boxnote}

\newpage
\begin{problem}
  Use MATLAB to create a plot of the solution to the differential equation $\dfrac{dy}{dx} = \dfrac{1}{x^2-1}$ which also satisfies $y(2) = 1$. Use the plot to describe the behaviour of this solution.
\end{problem}

% \begin{proof}[Solution]
%   Integration gives $\int \frac{dy}{dx} \; dx = \int \; dt \;\; \Longrightarrow
%   \;\; y = \int \frac{1}{x^2-1} \; dx$.  Hence, partial fractions yields
%   \begin{align*}
%     y &= \int \frac{1}{x^2-1} \;dx = \int \frac{1}{(x-1)(x+1)} \; dx = \int
%     \frac{1}{2(x-1)} - \frac{1}{2(x-1)} \; dx \\ &= \frac{1}{2} \ln|x-1| + -
%     \frac{1}{2}\ln|x+1| + C \, .
%   \end{align*}
%   Therefore, the general solution is $y = \frac{1}{2} \ln \left| \frac{x-1}{x+1}
%   \right| + C$.
% \end{proof}

\newpage
\topic{Initial Value Problems}
\begin{boxnote}
  A first-order \textbf{initial value problem} is a differential
  equation $$y'(x) = F\bigl( x, y(x) \bigr)$$ together with a point
  $(x_0, y_0)$ in the domain of $F$ \\called the
  \textbf{initial condition}.  \\[1ex]
  A \textbf{solution} to an initial value problem is a function $y$
  that satisfies the differential equation and $y(x_0) = y_0$.  This
  solution will be a \textbf{particular solution} to the differential
  equation.
\end{boxnote}

\begin{problem}
  Find the solution to the differential equation $\dfrac{dy}{dt} = 2t+1$; $y(0) = 3$. Now use MATLAB to create a plot of the solution, and use the plot to describe the behaviour of this solution.
\end{problem}
\newpage
\hfill$\dfrac{dy}{dt} = 2t+1$; $y(0) = 3$.

% \begin{proof}[Solution]
%   Integration gives $\int \frac{dy}{dt} \; dt = \int 2t +1 \; dt \;\;
%   \Longrightarrow \;\; y = t^2 +t + C$.  Since $3 = y(0) = C$, the unique
%   solution is $y = t^2 + t +3$.
% \end{proof}

\newpage

\begin{problem}
  Use MATLAB to create a plot of the solution to the initial value problem: $\dfrac{dy}{dx} = x^2 e^x$; $y(0) = 2$. Describe the behaviour of the solution.
\end{problem}


% \begin{proof}[Solution]
%   Integration gives $\int \frac{dy}{dx} \; dx = \int \; dt \;\; \Longrightarrow
%   \;\; y = \int x^2 e^x \; dx$.  Hence, integration by parts yields
%   \[
%   y = \int x^2 e^x \;dx = x^2 e^x - 2 \int x e^x \; dx = x^2 e^x - 2 \bigl( x
%   e^x + \int e^x \; dx \bigr) = x^2 e^x -2 x e^x + 2 e^x + C \, .
%   \]
%   Since $2 = y(0) = 2+C$, we have $C = 0$ and the unique solution is $y = x^2e^x
%   -2x e^x +2 e^x$.
% \end{proof}

\newpage
%\topic{Verifying Solutions}
%\begin{boxnote}
%  As with `regular' (algebraic, or {\bf non}-differential) equations,
%  solving a differential equation from scratch can be very difficult.
%  It is much easier to verify whether a possible solution (given or
%  guessed at) is correct.
%\end{boxnote}
%
%\begin{problem}
%  Determine which of the functions \\[.5ex]
%$$y_1 = k e^t \mbox{~~~~or~~~~} y_2 = e^{kt}$$\\[.5ex]
%is actually a solution to  $\dfrac{dy}{dt} = k y$.
%\end{problem}
%
%\newpage
%\begin{problem}
%  Show that $g(x) = \sin(x) - \cos(x)$ is a solution to the initial value
%  problem:
%  \begin{xalignat*}{3}
%    \frac{d^2y}{dx^2} + y &= 0 & y(0) &= -1 & y'(0) &= 1
%    \, .
%  \end{xalignat*}
%\end{problem}
%
%\newpage



% \begin{proof}[Solution]
%   Since $g'(x) = \cos(x) + \sin(x)$ and $g''(x) = -\sin(x) +\cos(x) = - g(x)$,
%   we see that $g(x)$ is a particular solution to the differential equation.  As
%   $g(0) = -\cos(0) = -1$ and $g'(0) = \cos(0) = 1$, it also satisfies the
%   initial conditions.
% \end{proof}

%\newpage

% \topic{Existence and Uniqueness}
% \section*{Existence and Uniqueness}

% \noindent
% When does a first-order initial value problem have a solution?
%
%\newpage
% \topic{Visualizing Solutions}
% \subsection*{Visualizing Solutions}

% When considering a differential equation with only a first derivative, recall that:
% \begin{center}
% $\dfrac{dy}{dx}$ represents the \define{slope} of $y(x)$.
% \end{center}
% For $\dfrac{dy}{dx} = f(x,y)$, a \define{slope field} is obtained by
% drawing a line segment in the direction $\bigl( 1, f(x,y) \bigr)$ from
% the point $(x,y)$.  A \define{particular solution} must be tangent to the
% slope field at each point.

% \newpage

% \begin{problem}
%   Draw a few of the line segments in the slope field for  \\ 
%   $\dfrac{dy}{dx} = - \dfrac{x}{y}$.
% \end{problem}
% \mypstrickspic{Graphs/week01_empty_slope_field2}

% \begin{proof}[Solution]
%   On the $y$-axis, the slope is $0$; on the $x$-axis, the line segments are
%   vertical and the slope is infinite; at the origin the slope is undefined.

%   The slope field suggests that the solution curves are circles centered at the
%   origin.  To verify this, we differentiate implicitly
%   \[
%   0 = \frac{d}{dx} \bigl( x^2 + y^2 - r^2 \bigr) = 2x + 2y \frac{dy}{dx} \;\;
%   \Longrightarrow \;\; \frac{dy}{dx} = - \frac{x}{y} \, .  \qedhere
%   \]
% \end{proof}

%\newpage

% Below is a more complete slope field for $\dfrac{dy}{dx} = -
% \dfrac{x}{y}$.  Using the slope field, guess the formulae for the
% solution curves.
%   \begin{center}
%     \includegraphics[width=10.5cm,height=10.5cm]{Graphs/slopeField0}
%   \end{center}

% \newpage
% \begin{minipage}{5in}
% Verify your proposed solution to the DE $\dfrac{dy}{dx} = - \dfrac{x}{y}$.
% \end{minipage}
% \begin{minipage}{8cm}
%     \includegraphics[width=8cm,height=8cm]{Graphs/slopeField0}
% \end{minipage}


% \topic{Euler's Method}
% \subsection*{Euler's Method / Geometric Idea}

% Fix a starting $A_0$ coming from the initial conditions.  From the differential
% equation, we know the slope of the tangent line.  Take a small step along the
% tangent line to $A_1$.  Along this small step, the slope does not change too
% much, so $A_1$ is close to the solution curve.  Iterating this process, we have
% a polygonal approximation for the solution curve.


% \vspace*{0.5em} 
% \comment{Building on this idea, we obtain}

% \begin{boxnote}
%   Consider the initial value problem $\dfrac{dy}{dt} = f(x,y)$; $y(x_0) = y_0$.
%   If $f$ and $\dfrac{\partial f}{\partial y}$ are continuous in some
%   neighbourhood of $(x_0,y_0)$, then the initial value problem has a unique
%   solution in some neighbourhood of $x_0$.
% \end{boxnote}

% \newpage

% \begin{problem}
%   Does this theorem apply to the following initial value problem:
%   $\dfrac{dy}{dx} = x^2-y; y(1) = 6$?
% \end{problem}

% \begin{proof}[Solution]
%   Since $f(x,y) = x^2-y$ and $f_y(x,y) = 1$ are both continuous on $\RR^2$, the
%   hypothesis of the theorem are satisfies, so there is a unique solution in the
%   interval $1-h < x <1-h$ for some positive number $h$.  In fact, the general
%   solution is $y = x^2-2x+2+Ce^{-x}$ because
%   \[
%   \frac{dy}{dx} = 2x-2-Ce^{-x} = x^2 - \bigl( x^2-2x+2+Ce^{-x} \bigr) = x^2-y \, .
%   \]
%   Moreover, $y(1) = 6$ if and only if $C = 5e$.
% \end{proof}

% \newpage
% \begin{problem}
%   Does this theorem apply to the following initial value problem:
%   $\dfrac{dy}{dt} = 3y^{2/3}; y(2) = 0$?
% \end{problem}

% \begin{proof}[Solution]
%   We have $f(x,y) = 3y^{2/3}$ and $f_y(x,y) = 2y^{-1/3}$.  However, $f_y(x,y)$
%   is not continuous (or even defined) at $y = 0$.  Consequently, there is no
%   neighbourhood of $(2,0)$ in which both $f$ and $f_y(x,y)$ are continuous, so
%   the theorem doesn't apply.

%   In this case, the initial value problem does not have a unique solution.
%   Indeed, both $y_1(x) = 0$ and $y_2(x) = (x-2)^3$ satisfy the initial value
%   problem.  \comment{The reason for nonuniqueness in this case is that the
%     velocity decreases too slowly when approaching the equilibrium position.  As
%   a result, the solution manages to reach the singular point in a finite time.}
% \end{proof}

% The slope fields for these differential equations are:

% %\includegraphics[width=6.5cm]{./Graphs/slopeField1.png}
% \hfill
%\includegraphics[width=6.5cm]{./Graphs/slopeField2.png}

% \topic{Picard's Method}
% \subsection*{Picard's Method}

% Consider the sequence of functions
% \begin{xalignat*}{2}
%   g_0(x) &:= y_0 \, , & g_i (x) &:= y_0 + \int_{x_0}^{x} f\bigl( u, g_{i-1}(u)
%   \bigr) du \, ,
% \end{xalignat*}
% Prove that the sequence converges and that the limit is a solution.

% \newpage
% \topic{Visualizing Solutions - Long-Term Behaviour}
% \begin{problem}
%   Consider $\dfrac{dy}{dt} = 2-y$.  By constructing a slope field,
%   what can you say about the long-run behaviour of $y$ (i.e. what
%   happens to $y$ as $t \to \infty$, and does the same thing happen to
%   all solutions)?
% \end{problem}

% \mypstrickspic{Graphs/week01_empty_slope_field1}



% \begin{proof}[Solution]
%   From the slope field, we see that all solution curves have $y=2$ as a
%   horizontal asymptote; i.e.\ $\lim\limits_{t \to \infty} y = 2$.
%   \begin{center}
% %    \includegraphics[width=6.5cm]{./Graphs/slopeField3.png}
%   \end{center}
%   When $y = 2$, we have $\dfrac{dy}{dt} = 0$, so the solution curve doesn't
%   change; when $y > 2$, we have $\dfrac{dy}{dt} < 0$ and the solution curve
%   decreases; when $y < 2$, we have $\dfrac{dy}{dt} > 0$ and the solution curve
%   increases.
% \end{proof}

\newpage
\topic{Separable Differential Equations}
\section*{Separable Equations}

\noindent
We now introduce our first technique for solving first-order
differential equations.

If the right side of the equation $\dfrac{dy}{dx} = f(x,y)$ is the
product of a function in $x$ and a function in $y$, then the equation
is \textbf{separable}.  By separating the variables and integrating,
we obtain an implicit solution to the differential equation.

\begin{problem}
  Solve $\dfrac{dy}{dx} = \dfrac{x-5}{y^2}$, with y(1) = 4. Use MATLAB to create a plot of the solution.
\end{problem}
%\newpage
%\hfill   $\dfrac{dy}{dx} = \dfrac{x-5}{y^2}$.


% \begin{proof}[Solution]
%   We have 
%   \[
%   \int y^2 \; dy = \int (x-5) \; dx \;\; \Longrightarrow \;\; \tfrac{1}{3} y^3 =
%   \tfrac{1}{2}x - 5x + C \;\; \Longrightarrow \;\; y = \left( \tfrac{3}{2} x^2 -
%     15 x + C \right)^{1/3}  \, . \qedhere
%   \]
% \end{proof}

\newpage
\topic{Separable Equations - Example 1}
\begin{problem}
  Use MATLAB to create a plot of the solution to the initial value problem $\dfrac{dx}{dt} = \dfrac{x-1}{t+3}$; $x(-1) =
  0$.
\end{problem}

%\newpage
%\hfill $\dfrac{dx}{dt} = \dfrac{x-1}{t+3}$; $x(-1) = 0$

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \[
%   \int \frac{dx}{x-1} = \int \frac{dt}{t+3} \;\; \Longrightarrow \;\; \ln | x-1
%   | = \ln |t+3| + C  \, . 
%   \]
%   Since $x(-1) = 0$, it follows that $0 = \ln|1| = \ln|0-1| = \ln|-1+3| +C =
%   \ln(2) +C$, so $C = - \ln(2)$.  Hence, near $x(-1) = 0$, we obtain
%   \[
%   \ln(1-x) = \ln(t+3) - \ln(2) = \ln\left(\tfrac{t+3}{2} \right) \;\;
%   \Longrightarrow \;\; 1-y = \tfrac{t+3}{2} \;\; \Longrightarrow \;\; -
%   \tfrac{1}{2}(x+1) \, . \qedhere
%   \]
% \end{proof}

\newpage
%\topic{Separable Equations - Example 2}
%\begin{problem}
%  Solve $\dfrac{dz}{dr} = \dfrac{6 r^5 -2 r +1}{\cos(z) + e^z}$.
%\end{problem}
%
%\newpage
%
%  \hfill  $\dfrac{dz}{dr} = \dfrac{6 r^5 -2 r +1}{\cos(z) + e^z}$.

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \[
%   \int \cos(z) + e^z \; dz = \int 6r^5-2r+1 \; dr \;\; \Longrightarrow \;\;
%   \sin(z) + e^z = r^6-r^2+r+C \, .
%   \]
%   In this case, one cannot solve explicitly for $z$ in terms of $r$.
% \end{proof}

%\newpage
%\topic{Separable Equations - The Exponential DE}
%\begin{problem}
%  Solve $\dfrac{dy}{dx} = ky$.  Note: This equation describes exponential
%    growth or decay.
%\end{problem}

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \[
%   \int \frac{dy}{y} = \int k \; dx \;\; \Longrightarrow \;\; \ln|y| = kx + C
%   \;\; \Longrightarrow \;\; y = e^C e^{kx} = A e^{kx} \, . \qedhere
%   \]
% \end{proof}

%\newpage
%\topic{Separable Equations - Heating and Cooling}
%\begin{problem}
%  Newton's law of cooling states that the rate of change in the temperature $H$
%  of a body is proportional to the difference between its temperature $H$ and
%  that of its surroundings $S$.  Solve $$\dfrac{dH}{dt} = k(S-H)$$  where H(0)
%\end{problem}
%%
%\newpage
%\hfill $\dfrac{dH}{dt} = k(S-H)$   \\
%\vspace{3in}
%What happens to the temperature $H$ as $t \to \infty$?

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \[
%   \int \frac{dH}{S-H} = \int k \; dt \;\; \Longrightarrow \;\; - \ln|S-H| = kt +
%   K \;\; \Longrightarrow \;\; S-H = Ce^{-kt} \;\; \Longrightarrow \;\; H =
%   S-Ce^{-kt} \, .
%   \]
%   It follows that $\lim\limits_{t \to \infty} H = \lim\limits_{t \to \infty}
%   S-Ce^{-kt} = S$.
% \end{proof}

%\newpage
%\topic{Separable Equations - Explosion Equation}
%\begin{problem}
%  Solve the explosion equation: $\dfrac{dx}{dt} = x^2$ where $x(0) > 0$.  Give a
%  physical interpretation of the solution.  Note: This equations occurs in
%    chemistry when the rate of a reaction is proportional to the concentration
%    of the two reagents.
%\end{problem}

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \[
%   \int \frac{dx}{x^2} = \int \; dt \;\; \Longrightarrow \;\; - \frac{1}{x} =
%   t + C \;\; \Longrightarrow \;\; x = - \frac{1}{t+C} \, . 
%   \]
%   Since $0 < x(0) = - \frac{1}{C}$, we have $C < 0$.  It follows that the
%   solution curve has a vertical asymptote at $t = - C > 0$.  In other words, the
%   solution becomes infinitely large in finite time.  \comment{Physically, this
%     conclusion corresponds to the explosive nature of the process.  Typically
%     the mathematical model is inapplicable if $t$ is too close to $-C$.}
% \end{proof}

\newpage
\topic{Separable Equations - Logistic Equation}
\begin{problem}
 Use MATLAB to plot the solution to the logistic equation: $\dfrac{dP}{dt} = (1-P)P$, where P(0) = 0.1. Try different values for the initial condition, what happens to the solution as the initial condition changes?
\end{problem}
%\newpage
%\hfill $\dfrac{dP}{dt} = (1-P)P$

%\newpage
%\hfill $\dfrac{dP}{dt} = (1-P)P$

% \begin{proof}[Solution]
%   Separating the variables and integrating gives
%   \begin{align*}
%     \int \frac{dP}{(1-P)P} &= \int \; dt \;\; \Longrightarrow \;\; \int
%     \frac{1}{P} + \frac{1}{1-P} \; dP = t+C \;\; \Longrightarrow \;\; \ln|P| -
%     \ln|1-P| = t+C \\
%     \frac{P}{1-P} &= Ae^t \;\; \Longrightarrow \;\; (1+Ae^t)P = Ae^t \;\;
%     \Longrightarrow \;\; P = \frac{Ae^t}{1+Ae^t} \, . 
%   \end{align*}
%   More qualitatively, we see that the population has two equilibrium at $P = 0$
%   and $P = 1$.  For $0 < P < 1$, the population is increasing and for $P > 1$
%   the population is decreasing.  Hence, the equilibrium population $0$ is
%   unstable (as soon as a population arises it begins to grow), while the
%   equilibrium population $1$ is stable (a smaller population increases and a
%   larger one decreases).  Observe, that the solution curve has two horizontal
%   asymptotes:
%   \[
%   \lim_{t \to \infty} P = \lim_{t \to \infty} \frac{Ae^t}{1+Ae^t} = 1 \, ,
%   \qquad \lim_{t \to -\infty} P = \lim_{t \to -\infty} \frac{Ae^t}{1+Ae^t} = 0
%   \, . \qedhere
%   \]
% \end{proof}

% \newpage
%\topic{Separation of Variables - }
% \begin{problem}
%   Solve $\dfrac{dy}{dx} = \dfrac{x^2+3y^2}{2xy}$.
% \end{problem}
%
% \begin{proof}[Solution]
%   Set $w := \dfrac{y}{x}$ so $y = xw$, $\dfrac{dy}{dx} = w + x \dfrac{dw}{dx}$,
%   and
%   \begin{align*}
%     w &+ x \dfrac{dw}{dx} = \dfrac{dy}{dx} = \dfrac{x^2+3y^2}{2xy} =
%     \dfrac{1+3(y/x)^2}{2(y/x)} = \dfrac{1+3w^2}{2w} \;\; \Longrightarrow \;\; x
%     \dfrac{dw}{dx} = \dfrac{1+3w^2-2w^2}{2w} = \dfrac{1+w^2}{2w} \\
%     \;\; \Longrightarrow \;\; &\int \frac{2w}{1+w^2} \; dw = \int \dfrac{dx}{x}
%     \;\; \Longrightarrow \;\; \ln(1+w^2)= \ln|x| + K \;\; \Longrightarrow \;\;
%     1+w^2 = Cx \\ \;\; \Longrightarrow \;\; & 1+(y/x)^2 = Cx \;\;
%     \Longrightarrow \;\; x^2 + y^2 - Cx^3 = 0 \, . \qedhere
%   \end{align*}
% \end{proof}
\newpage

\topic{First-order Linear Equations}
\section*{First-order Linear Equations}

\noindent
How does one integrate first-order linear equations?

A \textbf{linear first-order equation} is one that can be expressed in the form
$$a_1(x) y' + a_0(x) y = b(x)$$
Assuming that $a_1(x) \neq 0$, we can rewrite the equation in the
\textbf{standard form}:
\[
\dfrac{dy}{dx} + P(x) y = Q(x) \, . 
\]
Note: when $P(x)$ and $Q(x)$ are constants, this equation is also
\textbf{separable} (seen earlier).

\newpage
\begin{problem}
  Use MATLAB to plot the solution to $\dfrac{dy}{dt} +2y = 3$ with y(3) = 4.
\end{problem}

% \begin{proof}[Solution]
%   Separating variables and integrating gives
%   \[
%   \frac{dy}{dt} = 3-2y \;\; \Longrightarrow \;\; \int \frac{dy}{3-2y} = \int \;
%   dt \;\; \Longrightarrow \;\; -\tfrac{1}{2} \ln|3-2y| = t + K \;\;
%   \Longrightarrow \;\; y = \tfrac{3}{2} + C e^{-2t} \, . \qedhere
%   \]
% \end{proof}

%\newpage 
%
%Although separation of variables method cannot be used in general,
%Euler popularized an alternative: 
%\begin{center}
%  Multiply the standard form by an appropriate {\em integrating
%    factor} so that resulting equation is easily integrable.
%\end{center}
%
%More specifically, we multiply our standard form by by $M(t)$ to obtain 
%$$M(t) \dfrac{dy}{dt} + 2 M(t) y = 3 M(t)$$  
%Can we choose $M(t)$ so that the left side is simply the derivative of
%one function?


% \begin{proof}[Solution]
% Since $\dfrac{d}{dt} \Bigl( M(t) y \Bigr) = M(t) \dfrac{dy}{dt} +
% \begin{proof}[Alternative Solution]
%   Multiply by $M(t)$ to obtain $M(t) \dfrac{dy}{dt} + 2 M(t) y = 3 M(t)$.  Can
%   we choose $M(t)$ so that the left side is simply the derivative of one
%   function?  Since $\dfrac{d}{dt} \Bigl( M(t) y \Bigr) = M(t) \dfrac{dy}{dt} +
%   M'(t) y$, we $M'(t) = 2M(t)$ or $M(t) = e^{2t}$.  Multiplying by the
%   integrating factor $e^{2t}$ yields
%   \[
%   \frac{d}{dt} \Bigl( e^{2t} y \Bigr) = e^{2t} \frac{dy}{dt} + 2e^{2t}y =
%   3e^{2t} \;\; \Longrightarrow \;\; e^{2t} y = \int 3e^{2t} \; dt =
%   \tfrac{3}{2}e^{2t} + C \;\; \Longrightarrow \;\; y = \tfrac{3}{2} + C e^{-2t}
%   \, . \qedhere
%   \]
% \end{proof}

%\newpage
%
%\topic{Integrating Factors}
%\section*{Integrating Factors}
%For $\dfrac{dy}{dt} + P(t) \, y = Q(t)$, the \textbf{integrating factor} is
%defined to be $$M(t) = e^{\bigl( \int P(t) \; dt \bigr)}.$$  
%
%Show how multiplying the DE by $M(t)$ results in the left side being the derivative of a product.

% $\dfrac{d}{dx} \Bigl( M(x) \Bigr) = \dfrac{d}{dx} \Bigl( \exp\bigl( \int P(x) \;
% dx \bigr) \Bigr) = M(x) P(x)$, multiplication and integration yield
% \begin{align*}
%   \dfrac{d}{dx} \Bigl( M(x) y \Bigr) &= M(x) \dfrac{dy}{dx} + M(x) P(x) y
%   = M(x) Q(x) \\
%   M(x) y &= \int M(x) Q(x) \; dx \;\; \Longrightarrow \;\; y = \exp\left( - \int
%     P(x) \; dx \right) \left[ \int M(x) Q(x) \; dx \right] \, .
% \end{align*}

\newpage
\begin{problem}
  Use MATLAB to plot the solution to $\dfrac{dy}{dx} +2 y = 3e^x$, with y(0) = 1 \\[1ex]
  Note: this is a linear DE, but it is not separable.
\end{problem}
%\newpage
%  \hfill $\dfrac{dy}{dx} +2 y = 3e^x$.  \\[1ex]

%\newpage
%\begin{problem}
%  Verify that the solution found satisfies $\dfrac{dy}{dx} +2 y = 3e^x$.  \\[1ex]
%\end{problem}
% \begin{proof}[Solution]
%   Since the integrating factor is $M(x) = \exp\left( \int 2 \; dx \right) =
%   e^{2x}$, we obtain
%   \begin{align*}
%     \dfrac{d}{dx}\bigl( e^{2x} y \bigr) &= e^{2x} \dfrac{dy}{dx} +2 e^{2x} y =
%     3e^{3x} \;\; \Longrightarrow \;\; e^{2x}y = \int 3e^{3x} \; dx = e^{3x} + C
%     \;\; \Longrightarrow \;\; y = e^{x} + Ce^{-2x} \, . \qedhere
%   \end{align*}
% \end{proof}

\newpage
\topic{Linear DEs - IVP Example 1}
\begin{problem}
  Use MATLAB to plot the solution to the initial value problem where  \\
$$\dfrac{1}{x} \dfrac{dy}{dx} -
  \dfrac{2y}{x^2} = x \cos(x)
\mbox{   and  } 
y\left(\tfrac{\pi}{2}\right) = 3$$
\end{problem}

%\newpage
%\hfill $\ds \dfrac{1}{x} \dfrac{dy}{dx} -
%  \dfrac{2y}{x^2} = x \cos(x)
%\mbox{   and  } 
%y\left(\tfrac{\pi}{2}\right) = 3$

%\newpage
%  Verify that the solution found satisfies 
%$\dfrac{1}{x} \dfrac{dy}{dx} - \dfrac{2y}{x^2} = x \cos(x)$.

\newpage

% \begin{proof}[Solution]
%   The standard form of this equation is $\dfrac{dy}{dx} - \dfrac{2}{x} y = x^2
%   \cos(x)$.  Since the integrating factor is $M(x) = \exp \left( \int -2x^{-1}
%     \; dx \right) = \exp \bigl( -2 \ln|x| \bigr) = x^{-2}$, we obtain
%   \begin{align*}
%     \dfrac{d}{dx}\Bigl( x^{-2} y \Bigr) &= x^{-2} \dfrac{dy}{dx} - \dfrac{2}{x}
%     y = \cos(x) \;\; \Longrightarrow \;\; x^{-2} y = \int \cos(x) \; dx =
%     \sin(x) + C \\ \;\; \Longrightarrow \;\; y &= x^2 \sin(x) + Cx^2 \, . 
%   \end{align*}
%   Setting $x = \tfrac{\pi}{2}$ yields $3 = \bigl( \tfrac{\pi}{2} \bigr)^2 \sin
%   \bigl( \tfrac{\pi}{2} \bigr) + C \bigl( \tfrac{\pi}{2} \bigr)^2 =
%   (1+C)\tfrac{\pi^2}{4}$, so $C = \tfrac{12}{\pi^2}-1$.  Therefore, the solution
%   is $y = x^2 \sin(x) + \bigl( \tfrac{12}{\pi^2}-1 \bigr) x^2$.
% \end{proof}
%
%\newpage
%\topic{Linear DEs - IVP Example 2}
%\begin{problem}
%  Solve $t x' + 2x = 4t^2$ and $x(1) = 2$.
%\end{problem}

%\newpage
%  \hfill $t x' + 2x = 4t^2$ and $x(1) = 2$.

%\newpage
%\begin{problem}
%  Verify that the solution found satisfies $t x' + 2x = 4t^2$ and
%  $x(1) = 2$.
%\end{problem}

%\begin{minipage}{6in}
%~\end{minipage}
%\begin{minipage}{3in}
%\begin{align*}
%t x' + 2x &= 4t^2\\
%x(1) & = 2
%\end{align*}
%\end{minipage}
% \begin{proof}[Solution]
%   The standard form of this equation is $\dfrac{dx}{dt} + \dfrac{2}{t} x = 4t$.
%   Since the integrating factor is $M(t) = \exp \left( \int 2t^{-1} \; dt \right)
%   = \exp \bigl( 2 \ln|t| \bigr) = t^2$, we obtain
%   \begin{align*}
%     \dfrac{d}{dt}\bigl( t^2 x \bigr) &= t^2 \dfrac{dx}{dt} + 2t x = 4t^3 \;\;
%     \Longrightarrow \;\; t^2 x = \int 4t^3 \; dx = t^4 + C \\ \;\;
%     \Longrightarrow \;\; x &= t^2 + C t^{-2} \, .
%   \end{align*}
%   Setting $t = 1$ yields $2 = 1+C$, so $C = 1$.  Therefore, the solution
%   is $x = t^2 -t^{-2}$.
% \end{proof}

%\newpage
% \topic{Linear DEs - Solution Design}
% \begin{problem}
%   Consider the initial value problem where 
% $$y' + \tfrac{2}{3}y = 1 - \tfrac{1}{2} x \mbox{ and }y(0) = y_0.$$  
% Find the value of $y_0$ for which the solution touches, but does not cross, the $x$-axis.
% \end{problem}

% \newpage
% \hfill $y' + \tfrac{2}{3}y = 1 - \tfrac{1}{2} x \mbox{ and }y(0) = y_0$



% % \begin{proof}[Solution]
%    Since the integrating factor is $M(x) = \exp \bigl( \int 2/3 \; dx \bigr) =
%    \exp \bigl( 2x/3 \bigr) = e^{2x/3}$, we obtain
%    \begin{align*}
%      \dfrac{d}{dt} \bigl( e^{2x/3} y \bigr) &= e^{2x/3} \dfrac{dy}{dx} +
%      \frac{2}{3} e^{2x/3} y = \Bigl( 1-\frac{1}{2}x \Bigr) e^{2x/3} \;\;
%      \Longrightarrow \;\; e^{2x/3} y = \int e^{2x/3} - \tfrac{1}{2}xe^{2x/3} \;
%      dx \\
%      \Longrightarrow \;\; e^{2x/3}y &= \tfrac{3}{2}e^{2x/3} -
%      \tfrac{3}{4}xe^{2x/3} + \int \tfrac{3}{4}e^{2x/3} \; dx =
%      \tfrac{3}{2}e^{2x/3} -
%      \tfrac{3}{4}xe^{2x/3} + \tfrac{9}{8} e^{2x/3} + C \\
%      \Longrightarrow \;\; y &= \tfrac{21}{8} - \tfrac{3}{4}x + Ce^{-2x/3} \, .
%    \end{align*}
%    Setting $x = 0$ yields $y_0 = \tfrac{21}{8} + C$ so $C = y_0 - \tfrac{21}{8}$.
%    The solution curve $y = \tfrac{21}{8} - \tfrac{3}{4}x + \bigl( y_0 -
%    \tfrac{21}{8} \bigr) e^{-2x/3}$ is tangent to the $x$-axis at $x_0$ provided
%    that we have both $0 = \tfrac{21}{8} - \tfrac{3}{4}x_0 + \bigl( y_0 -
%    \tfrac{21}{8} \bigr) e^{-2x_0/3}$ and $0 = - \tfrac{3}{4} - \tfrac{2}{3}
%    \bigl( y_0 - \tfrac{21}{8} \bigr) e^{-2x_0/3}$.  Hence, we obtain $\bigl( y_0
%    - \tfrac{21}{8} \bigr) e^{-2x_0/3} = -\tfrac{9}{8}$, $0 = \tfrac{12}{8} -
%    \tfrac{3}{4}x_0$, $x_0 = 2$, and $y_0 = -\tfrac{9}{8} e^{4/3} + \tfrac{21}{8}
%    \approx -1.642876$.
% % \end{proof}

%\newpage
%\topic{Linear DEs - Examining Solution Properties}
%\begin{problem}
%  Let $a$ and $\lambda$ be positive constants and let $b$ be any real number.
%  Show that every solution to $y' + ay = be^{-\lambda t}$ has the property that
%  $y \to 0$ as $t \to \infty$.
%\end{problem}

%\newpage
%
%~
%
%\newpage
%~

%\newpage

% \begin{proof}[Solution]
%   Since the integrating factor is $M(x) = \exp \bigl( \int a \; dt \bigr) = \exp
%   \bigl( at \bigr) = e^{at}$, we obtain
%   \begin{align*}
%     \dfrac{d}{dt} \bigl( e^{at} y \bigr) &= e^{at} \dfrac{dy}{dx} + a e^{at} y =
%     b e^{(a-\lambda) t} \;\; \Longrightarrow \;\; e^{at} y = \int be^{(a-\lambda
%       ) t} \; dt
%   \end{align*}
%   If $a \neq \lambda$, then we have $e^{at}y = \tfrac{b}{a-\lambda}
%   e^{(a-\lambda)t} + C$ so $y = \tfrac{b}{a-\lambda}
%   e^{-\lambda t} + C e^{-at}$.  Similarly, if $a = \lambda$, then we have
%   $e^{at}y = bt +C$ so $y = bte^{-at} + C e^{-at}$,  In either case, we see that
%   $y \to 0$ as $t \to \infty$.
% \end{proof}


%\newpage
%\topic{Applications - Tank}
%\section*{Applications}
%
%In the beginning slide of this week, we noted that many physical, chemical and biological
%behaviours can naturally be represented by differential equations.
%
%In the next few examples, we study the types of predictions that can
%be inferred from solutions to DEs that arise in applications.
%
%\newpage
%
%\begin{problem}
%  At time $t = 0$ a tank contains $Q_0$ kg of salt dissolved in 100 litres of
%  water.  Assume that water containing $1/4$ kg of salt per litre is entering
%  the tank at a rate of $r$ litres per minute, and that the well-stirred mixture
%  is draining from the tank at the same rate.
%  \begin{enumerate}
%  \item Set up the initial value problem that describes this flow process.
%
%%\newpage
%%~
%
%\newpage
%  \item If $r = 3$ and $Q_0 = 14$, use MATLAB to plot the solution to the initial value problem. What happens to the amount of salt as time goes on? Does it reach a certain amount and stay there, or does the level of salt grow forever?
%  \end{enumerate}
%
%%\newpage
%%~
%\end{problem}
%\newpage
% %\begin{proof}[Solution] $\;$
%
%   \begin{enumerate}
%   \item Assume that salt is neither created nor destroyed in the tank.  Hence,
%     variations in the amount of salt are due solely to the flows in and out of
%     the tank.  More precisely, we have
%     \begin{align*}
%       \dfrac{dQ}{dt} &= \text{rate in} - \text{rate out} = \bigl( \tfrac{1}{4}
%       \; \text{kg} \cdot \text{L}^{-1} \bigr)\bigl( r \; \text{L} \cdot
%       \text{min}^{-1} \bigr) - \bigl( \tfrac{Q}{100} \; \text{kg} \cdot
%       \text{L}^{-1} \bigr) \bigl( r \; \text{L} \cdot \text{min}^{-1} \bigr) =
%       \frac{r}{4} - \frac{r}{100} Q
%     \end{align*}
%     and $Q(0) = Q_0$.
%   \item Since the standard form is $\dfrac{dQ}{dt} + \dfrac{r}{100} Q =
%     \dfrac{r}{4}$, the integrating factor is
%     \[
%     M(t) := \exp \bigl( \int \tfrac{r}{100} \; dt \bigr) = e^{rt/100} \, .
%     \]
%     Multiplication and integration yield
%     \begin{align*}
%       \dfrac{d}{dt} \Bigl( e^{rt/100} Q \Bigr) &= e^{rt/100} \dfrac{dQ}{dt} +
%       \frac{r}{100} e^{rt/4} Q = \frac{r}{100}e^{rt/100} \\
%       e^{rt/100} Q &= \int \frac{r}{4}e^{rt/100} \; dt = 25e^{rt/100} + C \;\;
%       \Longrightarrow \;\; Q = 25 + C e^{-rt/100} \, . 
%     \end{align*}
%     Setting $t = 0$, we obtain $Q_0 = 25+C$ or $C = Q_0-25$, so $Q(t) = 25 +
%     (Q_0 -25)e^{-rt/100}$.  Moreover, $Q_L = \lim\limits_{t \to \infty} Q(t) =
%     25$ so the limiting concentration is $\tfrac{Q_L}{100} = \frac{1}{4} \;
%     \text{kg} \cdot \text{L}^{-1}$.
%   \item If $r = 3$ and $Q_0 = 2 Q_L = 50$, then the solution becomes $Q(t) = 25
%     +25 e^{-0.03 t}$.  Since $2\%$ of $25$ is $0.5$, we wish to find the time
%     $T$ at which $Q(t)$ has the value $25.5$;
%     \[
%     25.5 = 25 +25e^{-0.03T} \;\; \Longrightarrow \;\; T = \frac{\ln(50)}{0.03}
%     \approx 130.4 \; \text{min} \, .
%     \]
%     To determine $r$ so that $T = 45$ min, set $t = 45$, $Q_0 = 50$, $Q(t) =
%     25.5$ and solve for $r$:
%     \[
%     25.5 = 25 +25e^{45r/100} \;\; \Longrightarrow \;\; T = \frac{100 \ln(50)}{45}
%     \approx 8.69 \; \text{L} \cdot \text{min}^{-1} \, . 
%     \]
%   \end{enumerate}
%% \end{proof}
%\newpage
%
%\topic{Applications - Gravity-Fed Water Flow}
%\begin{problem}
%A titration buret (a tall, narrow cylinder used to measure liquids)
%has a hole in the bottom, out of which the liquids flow.  If the buret
%is filled with water, the height of the water in the buret drops at a
%rate proportional to the {\em square root} of the current water
%height.
%\end{problem}
%
%\begin{enumerate}[(1)]
%\item \label{buret_de} Let $h$ be the height of water in buret. Write
%  the differential equation for the rate of change of $h$ with respect
%  to the current height.  \vspace{1in}
%
%\item The differential equation in (a) has a constant of
%  proportionality in it.  Is that constant positive or negative?
% \vspace{1.5in}
%
%\newpage
%
%\item At what height $h$ does the water level become constant?
%  Explain why this height is reasonable in the context of the problem.
%  \vspace{1.5in}
%
%%\newpage
%%
%%\item Find the general solution to the differential equation.
%
%\newpage
%\item The buret originally is filled up with water to a height of 16
%  cm, and after 10 seconds the water level has dropped to a height of
%  4 cm.  Determine the time at which the buret will be empty.
%\end{enumerate}
%
%\newpage
%\topic{Applications - Pond}
%\begin{problem}
%  Consider a pond that initially contains $10$ million litres of fresh water.
%  Water containing an undesirable chemical flows into the pond at the rate of
%  $5$ million litres per year; the mixture in the pond flows out at the same
%  rate.  The concentration $\gamma(t)$ of chemical in the incoming water varies
%  periodically with time according to the expression $\gamma(t) = 2 + \sin(2t)
%  \; \text{g} \cdot \text{L}^{-1}$.
%  \begin{enumerate}
%  \item Construct a mathematical model of this flow process.
%\newpage
%  \item Determine the amount of chemical in the pond at any time.
%
%%\newpage
%%~
%\newpage
%  \item Describe the effect of the variation in the incoming concentration.
%  \end{enumerate}
%\end{problem}
%
%%\newpage
%%~ 
%\newpage
%
%   \begin{enumerate}
%   \item Let $t$ denote time measured in years and $Q(t)$ the amount of chemical
%     measured in grams.
%     \begin{align*}
%       \dfrac{dQ}{dt} &= \text{rate in} - \text{rate out} = \bigl( 2 + \sin(2t)
%       \; \text{g} \cdot \text{L}^{-1} \bigr) \bigl( 5 \times 10^6 \; \text{L}
%       \cdot \text{a}^{-1} \bigr) - \bigl( \tfrac{Q}{10^7} \; \text{g} \cdot
%       \text{L}^{-1} \bigr) \bigl( 5 \times 10^6 \; \text{L} \cdot \text{a}^{-1}
%       \bigr) \\ &= \bigl( 5 \times 10^6 \bigr)\bigl(2 + \sin(2t) \bigr) -
%       \frac{Q}{2}
%     \end{align*}
%     For convenience, let $q(t) = Q(t) 10^{-6}$, so $\dfrac{dq}{dt} +
%     \dfrac{1}{2}q = 10 +5 \sin(2t)$.  To begin, there is no chemical in the
%     pond, so $q(t) = 0$.
%   \item  Since the integrating factor $M(t) := \exp\bigl( \frac{1}{2} \; dt
%     \bigr) = e^{t/2}$, we have
%     \begin{align*}
%       \frac{d}{dt} \Bigl( e^{t/2} q \Bigr) &= e^{t/2} \dfrac{dq}{dt} +
%       \frac{1}{2}e^{t/2} q = 10 e^{t/2} + 5 e^{t/2} \sin(2t) \\
%       e^{t/2} q &= \int 10 e^{t/2} + 5 e^{t/2} \sin(2t) \; dt = 20e^{t/2} + 5
%       \int e^{t/2} \sin(2t) \; dt
%     \end{align*}
%     Using integration by parts twice, we have
%     \begin{align*}
%       \int e^{t/2} \sin(2t) \; dt &= 2e^{t/2}\sin(2t) - \int 4e^{t/2} \cos(2t)
%       \;dt \\ &= 2e^{t/2}\sin(2t) - 8 e^{t/2} \cos(2t) - \int 16e^{t/2} \sin(2t)
%       \;dt
%     \end{align*}
%     so $q(t) = 20 + \tfrac{10}{17} \sin(2t) - \tfrac{40}{17} \cos(2t) +
%     Ce^{-t/2}$.  Setting $t = 0$ yields $0 = 20 - \tfrac{40}{17} + C$ so $C = -
%     \tfrac{300}{17}$ and $q(t) = 20 + \tfrac{10}{17} \sin(2t) - \tfrac{40}{17}
%     \cos(2t) + \tfrac{300}{17} e^{-t/2}$.
%   \item The exponential term in the solution is important for small $t$, but
%     diminishes rapidly as $t$ increases.  Later, the solution consists of an
%     oscillation, due to the $\sin(2t)$ and $\cos(2t)$ terms, about the constant
%     level $q=20$.  If the $\sin(2t)$ term were not present in the differential
%
%   \end{enumerate}
%
%
%\newpage
%\textbf{Remark}
%  Our model rests on several implicit assumptions.  First, the amount of water
%  is controlled entirely by the rates of flow in and out --- none is lost by
%  evaporation or by seepage into the ground or gained by rainfall.  Second, the
%  same is also true of the chemical --- none is absorbed by fish or other
%  organisms.  Third, the concentration of chemical is uniform throughout the
%  pond.  Whether the results obtained from the model are accurate depend
%  strongly on the validity of these assumptions.


\end{document}
