% =============================================================================
% ch03_qualitative.tex -- Chapter 3: Qualitative Analysis and Numerical Methods
% =============================================================================

\section{Qualitative Analysis and Numerical Methods}
\label{ch:qualitative}

\subsection{Autonomous Equations and Phase Lines}
\label{sec:ch03_autonomous}

An \textbf{autonomous differential equation} is a first-order ODE in which the right-hand side depends only on the dependent variable and \emph{not} explicitly on the independent variable~$t$:
\begin{equation}
  \label{eq:autonomous}
  \frac{\mathrm{d}y}{\mathrm{d}t} = f(y).
\end{equation}
Because $f$ has no explicit $t$-dependence, the direction field is invariant under horizontal translation. Solutions simply shift left or right along the $t$-axis without changing shape.

\paragraph{Equilibrium solutions.}
An \textbf{equilibrium solution} (or \textbf{critical point}) is a constant solution $y(t) \equiv y^*$ that satisfies
\begin{equation}
  f(y^*) = 0.
\end{equation}
At such a point the derivative vanishes and the solution is stationary. Every root of $f(y) = 0$ gives one equilibrium.

\paragraph{Phase line construction.}
The \textbf{phase line} is a one-dimensional sketch of the dynamics of \cref{eq:autonomous}. It is constructed in three steps:
\begin{enumerate}
  \item \textbf{Identify equilibria:} Solve $f(y) = 0$.
  \item \textbf{Determine the sign of $f(y)$} on each interval between consecutive equilibria.
        If $f(y) > 0$ on an interval, solutions increase ($y$ moves upward on the phase line).
        If $f(y) < 0$, solutions decrease.
  \item \textbf{Draw the phase line:} Mark each equilibrium on a vertical line and place arrows on each interval indicating the direction of motion.
\end{enumerate}

\begin{workedexample}
  \textbf{Phase line for $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y(y-1)(y-2)$.}

  \textit{Step 1: Equilibria.} Set $f(y) = y(y-1)(y-2) = 0$. The roots are
  \[
    y_1^* = 0,\qquad y_2^* = 1,\qquad y_3^* = 2.
  \]

  \textit{Step 2: Sign of $f(y)$ on each interval.} Test a sample point in each interval:
  \[
    \begin{array}{c|c|c}
      \text{Interval} & \text{Sample } y & f(y) \\ \hline
      (-\infty, 0) & y = -1 & (-)(-)(-) = - \;\; \text{(decreasing)} \\
      (0, 1)       & y = 0.5 & (+)(-)(-) = + \;\; \text{(increasing)} \\
      (1, 2)       & y = 1.5 & (+)(+)(-) = - \;\; \text{(decreasing)} \\
      (2, \infty)  & y = 3   & (+)(+)(+) = + \;\; \text{(increasing)}
    \end{array}
  \]

  \textit{Step 3: Phase line.}
  \begin{center}
  \begin{tikzpicture}[scale=0.9]
    % Vertical phase line
    \draw[thick] (0,0) -- (0,5);
    \draw[->] (0,0) -- (0,-0.3);
    \draw[->] (0,5) -- (0,5.3);

    % Equilibrium points
    \filldraw[black] (0,0.8)  circle (2.5pt) node[right=4pt] {$y^*=0$};
    \filldraw[black] (0,2.5)  circle (2.5pt) node[right=4pt] {$y^*=1$};
    \filldraw[black] (0,4.2)  circle (2.5pt) node[right=4pt] {$y^*=2$};

    % Arrows on intervals
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,0.35) -- (-0.15,0.0);
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,1.6) -- (-0.15,1.25);
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,3.3) -- (-0.15,2.95);
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,4.8) -- (-0.15,4.55);

    % Labels
    \node[below] at (0,-0.5) {$y$};
  \end{tikzpicture}
  \end{center}

  Solutions starting between $0$ and $1$ approach $y=0$ from above or $y=1$ from below as $t$ increases.
\end{workedexample}

\begin{workedexample}
  \textbf{Phase line for $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y^2 - 4$.}

  \textit{Step 1:} $y^2 - 4 = 0 \implies y^* = -2,\; +2$.

  \textit{Step 2:} Test intervals:
  \[
    \begin{array}{c|c|c}
      \text{Interval} & \text{Sample } y & f(y) \\ \hline
      (-\infty, -2) & y = -3 & 9 - 4 = +5 \;\; \text{(increasing)} \\
      (-2, 2)       & y = 0  & 0 - 4 = -4 \;\; \text{(decreasing)} \\
      (2, \infty)   & y = 3  & 9 - 4 = +5 \;\; \text{(increasing)}
    \end{array}
  \]

  \textit{Step 3:}
  \begin{center}
  \begin{tikzpicture}[scale=0.9]
    \draw[thick] (0,0) -- (0,5);
    \draw[->] (0,0) -- (0,-0.3);
    \draw[->] (0,5) -- (0,5.3);

    \filldraw[black] (0,1.25) circle (2.5pt) node[right=4pt] {$y^*=-2$};
    \filldraw[black] (0,3.75) circle (2.5pt) node[right=4pt] {$y^*=2$};

    % Arrow: below -2, going up
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,0.5) -- (-0.15,0.85);
    % Arrow: between -2 and 2, going down
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,3.0) -- (-0.15,2.2);
    % Arrow: above 2, going up
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,4.5) -- (-0.15,4.15);

    \node[below] at (0,-0.5) {$y$};
  \end{tikzpicture}
  \end{center}

  The interval $(-2,2)$ is a ``funnel'' where all solutions decrease toward $y=-2$ as $t \to \infty$.
\end{workedexample}

\begin{workedexample}
  \textbf{Phase line for $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y^2$.}

  \textit{Step 1:} $y^2 = 0 \implies y^* = 0$ (the only equilibrium).

  \textit{Step 2:} For $y \neq 0$, $y^2 > 0$, so $f(y) > 0$ on both $(-\infty,0)$ and $(0,\infty)$. Solutions increase on both sides of the equilibrium.

  \textit{Step 3:}
  \begin{center}
  \begin{tikzpicture}[scale=0.9]
    \draw[thick] (0,0) -- (0,5);
    \draw[->] (0,0) -- (0,-0.3);
    \draw[->] (0,5) -- (0,5.3);

    \filldraw[black] (0,2.5) circle (2.5pt) node[right=4pt] {$y^*=0$};

    % Arrow below 0: going up (toward 0)
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,1.5) -- (-0.15,1.15);
    % Arrow above 0: going up (away from 0)
    \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,3.8) -- (-0.15,3.45);

    \node[below] at (0,-0.5) {$y$};
  \end{tikzpicture}
  \end{center}

  Solutions approach $y^*=0$ from below but depart from above. This is the hallmark of a \textbf{semi-stable} equilibrium (discussed in \cref{sec:ch03_stability}).
\end{workedexample}

\subsection{Stability Analysis}
\label{sec:ch03_stability}

\paragraph{Linear stability test.}
Suppose $y^*$ is an equilibrium of $\mathrm{d}y/\mathrm{d}t = f(y)$. Expand $f$ in a Taylor series about $y^*$:
\begin{equation}
  f(y) = f(y^*) + f'(y^*)(y - y^*) + \frac{f''(y^*)}{2}(y - y^*)^2 + \cdots.
\end{equation}
Since $f(y^*) = 0$, the linearized equation near $y^*$ is
\begin{equation}
  \label{eq:linearized}
  \frac{\mathrm{d}u}{\mathrm{d}t} = f'(y^*)\,u, \qquad u = y - y^*.
\end{equation}
The solution is $u(t) = u(0)\,e^{f'(y^*) t}$, which immediately yields three cases.

\begin{keyresult}
  \textbf{Stability classification for autonomous equations $\mathrm{d}y/\mathrm{d}t = f(y)$.}

  Let $y^*$ be an equilibrium with $f(y^*) = 0$.

  \begin{center}
  \begin{tabular}{l l p{5.5cm}}
    \toprule
    \textbf{Type} & \textbf{Condition} & \textbf{Behavior} \\
    \midrule
    Asymptotically stable (sink)
      & $f'(y^*) < 0$
      & Nearby solutions converge to $y^*$ as $t \to \infty$. \\[4pt]
    Unstable (source)
      & $f'(y^*) > 0$
      & Nearby solutions depart from $y^*$ as $t$ increases. \\[4pt]
    Semi-stable (node)
      & $f'(y^*) = 0$
      & Solutions approach $y^*$ from one side and depart on the other. \\
    \bottomrule
  \end{tabular}
  \end{center}
\end{keyresult}

\begin{workedexample}
  \textbf{Classify the equilibria of $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y(y-1)(y-2)$.}

  We have $f(y) = y(y-1)(y-2) = y^3 - 3y^2 + 2y$, so
  \[
    f'(y) = 3y^2 - 6y + 2.
  \]

  Evaluate at each equilibrium:
  \begin{align*}
    f'(0) &= 3(0)^2 - 6(0) + 2 = 2 > 0
    &&\implies y^* = 0 \text{ is \textbf{unstable (source)}}. \\
    f'(1) &= 3(1)^2 - 6(1) + 2 = -1 < 0
    &&\implies y^* = 1 \text{ is \textbf{asymptotically stable (sink)}}. \\
    f'(2) &= 3(4) - 12 + 2 = 2 > 0
    &&\implies y^* = 2 \text{ is \textbf{unstable (source)}}.
  \end{align*}

  These classifications are consistent with the phase line drawn in the previous section: $y=1$ attracts solutions from both sides, while $y=0$ and $y=2$ repel.
\end{workedexample}

\begin{workedexample}
  \textbf{Semi-stable equilibrium: $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y^2$.}

  Here $f(y) = y^2$ and $f'(y) = 2y$. At the equilibrium $y^* = 0$:
  \[
    f'(0) = 0.
  \]
  The linear test is inconclusive (derivative is zero), so we examine the sign of $f(y)$ directly:
  $f(y) = y^2 > 0$ for all $y \neq 0$. Solutions approach $y^*=0$ from below but depart to the right from above. This confirms the \textbf{semi-stable (node)} classification.
\end{workedexample}

\begin{workedexample}
  \textbf{All three types in one equation: $\displaystyle\frac{\mathrm{d}y}{\mathrm{d}t} = y(y-1)^2$.}

  Equilibria: $y^* = 0$ and $y^* = 1$.

  Compute $f'(y) = (y-1)^2 + 2y(y-1) = (y-1)(3y-1)$.

  \begin{align*}
    f'(0) &= (-1)(-1) = 1 > 0
    &&\implies y^*=0 \text{ is \textbf{unstable (source)}}. \\
    f'(1) &= (0)(2) = 0
    &&\implies \text{linear test inconclusive.}
  \end{align*}

  For $y^*=1$, inspect the sign of $f(y) = y(y-1)^2$:
  Since $(y-1)^2 \geq 0$ always and $y > 0$ near $y=1$, we have $f(y) > 0$ on both sides of $y=1$.
  Solutions increase on both sides, so $y^*=1$ is \textbf{semi-stable} (approached from below, departed from above).
\end{workedexample}

\subsection{The Logistic Equation}
\label{sec:ch03_logistic}

\paragraph{Biological motivation.}
The simplest population model is exponential growth, $\mathrm{d}P/\mathrm{d}t = rP$, which predicts unbounded growth. In reality, resources are finite. The \textbf{logistic equation} introduces a carrying capacity $K > 0$ that caps the population:
\begin{equation}
  \label{eq:logistic}
  \frac{\mathrm{d}y}{\mathrm{d}t} = r\,y\left(1 - \frac{y}{K}\right), \qquad r > 0,\; K > 0.
\end{equation}
The factor $(1 - y/K)$ reduces the per-capita growth rate as $y \to K$. When $y \ll K$ the dynamics are approximately exponential; when $y \to K$ the growth rate vanishes.

\paragraph{Phase line analysis.}
Set $f(y) = ry(1 - y/K)$. The equilibria are:
\begin{align*}
  f(y) &= 0 \implies y\left(1 - \frac{y}{K}\right) = 0 \implies y^*_1 = 0,\quad y^*_2 = K.
\end{align*}

Compute the derivative:
\[
  f'(y) = r - \frac{2r}{K}y.
\]

Evaluate at each equilibrium:
\[
  f'(0) = r > 0 \;\;\text{(unstable source)}, \qquad
  f'(K) = r - 2r = -r < 0 \;\;\text{(asymptotically stable sink)}.
\]

\begin{center}
\begin{tikzpicture}[scale=0.9]
  \draw[thick] (0,0) -- (0,5);
  \draw[->] (0,0) -- (0,-0.3);
  \draw[->] (0,5) -- (0,5.3);

  \filldraw[black] (0,0.6)  circle (2.5pt) node[right=4pt] {$y^*=0$ \small (unstable)};
  \filldraw[black] (0,4.4)  circle (2.5pt) node[right=4pt] {$y^*=K$ \small (stable)};

  % Arrow between 0 and K: going up toward K
  \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,2.5) -- (-0.15,2.15);
  % Arrow below 0: going down (y < 0 is non-physical but mathematically valid)
  \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,0.2) -- (-0.15,-0.05);
  % Arrow above K: going down toward K
  \draw[-{Latex[length=4mm]}, red!70!black, very thick] (-0.15,4.9) -- (-0.15,4.65);

  \node[below] at (0,-0.5) {$y$};
\end{tikzpicture}
\end{center}

All solutions with $y(0) > 0$ approach the carrying capacity $K$ as $t \to \infty$.

\paragraph{Exact solution by separation of variables.}
We solve \cref{eq:logistic} with initial condition $y(0) = y_0$:
\begin{align*}
  \frac{\mathrm{d}y}{\mathrm{d}t}
  &= r\,y\left(1 - \frac{y}{K}\right) \\[6pt]
  \frac{\mathrm{d}y}{y\left(1 - \dfrac{y}{K}\right)}
  &= r\,\mathrm{d}t.
\end{align*}

Decompose the left-hand side using partial fractions:
\[
  \frac{1}{y\left(1 - \frac{y}{K}\right)}
  = \frac{K}{K y - y^2}
  = \frac{1}{y} + \frac{1}{K - y}.
\]
\textit{Verification:} $\displaystyle\frac{1}{y} + \frac{1}{K-y} = \frac{K-y+y}{y(K-y)} = \frac{K}{y(K-y)}$.

Integrate both sides:
\begin{align*}
  \int \left(\frac{1}{y} + \frac{1}{K-y}\right) \mathrm{d}y
  &= \int r\,\mathrm{d}t \\
  \ln|y| - \ln|K-y| &= rt + C \\
  \ln\left|\frac{y}{K-y}\right| &= rt + C \\
  \frac{y}{K-y} &= A\,e^{rt}, \qquad A = \pm e^C.
\end{align*}

Solve for $y$:
\begin{align*}
  y &= (K-y)\,A\,e^{rt} \\
  y(1 + A e^{rt}) &= K A e^{rt} \\
  y(t) &= \frac{K A e^{rt}}{1 + A e^{rt}}
        = \frac{K}{1 + \dfrac{1}{A}e^{-rt}}.
\end{align*}

Apply the initial condition $y(0) = y_0$:
\[
  y_0 = \frac{K}{1 + \tfrac{1}{A}}
  \implies 1 + \tfrac{1}{A} = \frac{K}{y_0}
  \implies \frac{1}{A} = \frac{K - y_0}{y_0}.
\]
Denoting $\displaystyle A' = \frac{K - y_0}{y_0}$, the final form is
\begin{equation}
  \label{eq:logistic_solution}
  y(t) = \frac{K}{1 + A' e^{-rt}},
  \qquad A' = \frac{K - y_0}{y_0}.
\end{equation}
As $t \to \infty$, $e^{-rt} \to 0$ and $y(t) \to K$, confirming the phase line prediction.

\begin{workedexample}
  \textbf{Logistic growth with $r = 0.5$, $K = 100$, $y_0 = 10$.}

  The equation is $\mathrm{d}y/\mathrm{d}t = 0.5\,y(1 - y/100)$.

  The constant $A'$ is
  \[
    A' = \frac{100 - 10}{10} = 9.
  \]
  The solution is
  \[
    y(t) = \frac{100}{1 + 9\,e^{-0.5t}}.
  \]

  At $t = 5$:
  \[
    y(5) = \frac{100}{1 + 9\,e^{-2.5}}
          = \frac{100}{1 + 9 \cdot 0.0821}
          = \frac{100}{1.739} \approx 57.5.
  \]
  The population reaches about 58\% of its carrying capacity after 5 time units.
\end{workedexample}

\subsection{Euler's Method}
\label{sec:ch03_eulers_method}

Many differential equations do not admit closed-form solutions. \textbf{Euler's method} provides a simple numerical procedure to approximate the solution.

\paragraph{Derivation from Taylor expansion.}
Let $y(t)$ be the exact solution of $y' = f(t,y)$. Expand $y(t_{n+1})$ about $t_n$ using Taylor's theorem:
\begin{equation}
  y(t_{n+1}) = y(t_n) + h\,y'(t_n) + \frac{h^2}{2}\,y''(\xi_n),
  \qquad \xi_n \in (t_n, t_{n+1}),
\end{equation}
where $h = t_{n+1} - t_n$ is the step size. Since $y'(t_n) = f(t_n, y(t_n))$,
\[
  y(t_{n+1}) = y(t_n) + h\,f(t_n, y(t_n)) + \frac{h^2}{2}\,y''(\xi_n).
\]
Euler's method retains only the first two terms, discarding the $O(h^2)$ remainder.

\begin{keyresult}
  \textbf{Euler's method.}
  Given $y' = f(t,y)$ with $y(t_0) = y_0$ and step size $h$:
  \begin{align}
    t_{n+1} &= t_n + h, \label{eq:euler_t} \\
    y_{n+1} &= y_n + h\,f(t_n, y_n). \label{eq:euler_y}
  \end{align}
\end{keyresult}

\begin{hintbox}
  \textbf{Error analysis.}

  \begin{itemize}
    \item \textbf{Local truncation error} (error per step):
          \[ \tau_{n+1} = \frac{h^2}{2}\,y''(\xi_n) = O(h^2). \]
    \item \textbf{Global truncation error} (accumulated error at a fixed $t$ after $N = (t-t_0)/h$ steps):
          \[ \varepsilon_N = O(h). \]
  \end{itemize}

  Halving the step size roughly halves the global error, but doubles the number of steps.
\end{hintbox}

\begin{workedexample}
  \textbf{Approximate $y' = y$, $y(0) = 1$ using Euler's method with $h = 0.1$.}

  The exact solution is $y(t) = e^t$. Here $f(t,y) = y$, so the iteration is
  \[
    y_{n+1} = y_n + 0.1\,y_n = 1.1\,y_n.
  \]

  Starting from $y_0 = 1$:
  \begin{center}
  \begin{tabular}{c c c c c c}
    \toprule
    $n$ & $t_n$ & $y_n$ (Euler) & $y(t_n) = e^{t_n}$ (exact) & Local error $\tau$ & Cumulative error \\
    \midrule
    0 & 0.0 & 1.0000 & 1.00000 & $-$ & 0.0000 \\
    1 & 0.1 & 1.1000 & 1.10517 & 0.00059 & 0.0052 \\
    2 & 0.2 & 1.2100 & 1.22140 & 0.00061 & 0.0114 \\
    3 & 0.3 & 1.3310 & 1.34986 & 0.00063 & 0.0189 \\
    4 & 0.4 & 1.4641 & 1.49182 & 0.00066 & 0.0277 \\
    5 & 0.5 & 1.6105 & 1.64872 & 0.00069 & 0.0382 \\
    6 & 0.6 & 1.7716 & 1.82212 & 0.00072 & 0.0505 \\
    7 & 0.7 & 1.9487 & 2.01375 & 0.00075 & 0.0650 \\
    8 & 0.8 & 2.1436 & 2.22554 & 0.00079 & 0.0819 \\
    9 & 0.9 & 2.3579 & 2.45960 & 0.00083 & 0.1017 \\
    10 & 1.0 & 2.5937 & 2.71828 & 0.00087 & 0.1246 \\
    \bottomrule
  \end{tabular}
  \end{center}

  At $t = 1.0$, Euler's method gives $y_{10} = 2.5937$ while the exact value is $e \approx 2.7183$. The relative error is
  \[
    \frac{|2.71828 - 2.59374|}{2.71828} \approx 4.58\%.
  \]

  The error grows because each step's $O(h^2)$ local error compounds. With the same $h = 0.1$, the error at $t = 1$ is proportional to $h \cdot t \approx 0.1$. To reduce the error by a factor of 10, use $h = 0.01$.
\end{workedexample}

\begin{workedexample}
  \textbf{Euler's method on $y' = -2y$, $y(0) = 3$, with $h = 0.5$.}

  Here $f(t,y) = -2y$, so $y_{n+1} = y_n - h(2y_n) = y_n(1 - 2h)$.

  With $h = 0.5$: $y_{n+1} = y_n(1 - 1) = 0$ for $n \geq 1$. This is an \textbf{unstable} outcome: any numerical noise would blow up because $|1 - 2h| = 0$ is at the boundary.

  The exact solution is $y(t) = 3e^{-2t}$. At $t = 1$: $y(1) = 3e^{-2} \approx 0.406$.

  Euler with $h = 0.5$ gives $y_2 = 0$ at $t = 1$, which is far off.

  With a smaller step $h = 0.1$: $y_{n+1} = y_n(1 - 0.2) = 0.8\,y_n$.
  \[
    y_{10} = 3 \cdot (0.8)^{10} = 3 \cdot 0.1074 \approx 0.322.
  \]
  This is much closer to the exact $0.406$.

  \textit{Lesson:} For stiff equations or large negative eigenvalues, small step sizes are essential for Euler's method.
\end{workedexample}

\subsection{Applications}
\label{sec:ch03_applications}

\subsubsection{Newton's Law of Cooling}
\label{sec:ch03_cooling}

\textbf{Physical setup.} A hot object placed in a cooler environment loses heat at a rate proportional to the temperature difference between the object and the ambient medium.

\textbf{Model.} Let $T(t)$ be the object's temperature and $T_a$ the constant ambient temperature. Newton's law states:
\begin{equation}
  \label{eq:newton_cooling}
  \frac{\mathrm{d}T}{\mathrm{d}t} = k\,(T - T_a), \qquad k < 0.
\end{equation}
The equilibrium is $T^* = T_a$ (the object eventually reaches ambient temperature). Since $f'(T_a) = k < 0$, the equilibrium is asymptotically stable.

\begin{workedexample}
  \textbf{A cup of coffee cools from $90^\circ$C to $70^\circ$C in 10 minutes in a $20^\circ$C room. How long until it reaches $40^\circ$C?}

  \textit{Step 1: Solve the ODE.} Separate variables:
  \[
    \frac{\mathrm{d}T}{T - T_a} = k\,\mathrm{d}t
    \implies \ln|T - T_a| = kt + C
    \implies T(t) = T_a + (T_0 - T_a)e^{kt}.
  \]

  With $T_a = 20$ and $T_0 = 90$:
  \[
    T(t) = 20 + 70\,e^{kt}.
  \]

  \textit{Step 2: Determine $k$.} At $t = 10$:
  \[
    70 = 20 + 70\,e^{10k}
    \implies e^{10k} = \frac{50}{70} = \frac{5}{7}
    \implies k = \frac{1}{10}\ln\!\left(\frac{5}{7}\right) \approx -0.0336.
  \]

  \textit{Step 3: Find the time to reach $40^\circ$C.}
  \[
    40 = 20 + 70\,e^{-0.0336t}
    \implies e^{-0.0336t} = \frac{20}{70} = \frac{2}{7}.
  \]
  \[
    -0.0336t = \ln\!\left(\frac{2}{7}\right)
    \implies t = \frac{\ln(2/7)}{-0.0336}
    = \frac{-1.253}{-0.0336} \approx 37.3 \text{ minutes}.
  \]

  The coffee reaches $40^\circ$C after approximately \textbf{37.3 minutes}.
\end{workedexample}

\subsubsection{Mixing Problems}
\label{sec:ch03_mixing}

\textbf{Physical setup.} A tank contains a solution of salt and water. Brine flows in at a known concentration and rate, and the well-stirred mixture flows out at a known rate. We wish to track the amount of salt $Q(t)$ in the tank.

\textbf{Model.} Let $V(t)$ be the volume, $c_1$ the concentration of incoming brine, and $r_1$, $r_2$ the inflow and outflow rates. The rate of change of salt is
\begin{equation}
  \label{eq:mixing}
  \frac{\mathrm{d}Q}{\mathrm{d}t} = \underbrace{c_1 r_1}_{\text{rate in}}
  - \underbrace{\frac{Q(t)}{V(t)}\,r_2}_{\text{rate out}}.
\end{equation}
If $r_1 = r_2 = r$ then $V(t) = V_0$ is constant, and the equation becomes linear:
\begin{equation}
  \frac{\mathrm{d}Q}{\mathrm{d}t} + \frac{r}{V_0}\,Q = c_1 r.
\end{equation}

\begin{workedexample}
  \textbf{A tank holds 100~L of brine with 20~kg of dissolved salt. Brine containing 0.5~kg/L flows in at 3~L/min. The well-mixed solution flows out at 3~L/min. Find $Q(t)$ and the long-term salt content.}

  \textit{Parameters:} $V_0 = 100$~L, $Q(0) = 20$~kg, $c_1 = 0.5$~kg/L, $r = 3$~L/min.

  \textit{ODE:} Since inflow = outflow, $V(t) = 100$ is constant.
  \[
    \frac{\mathrm{d}Q}{\mathrm{d}t} = (0.5)(3) - \frac{Q}{100}(3)
    = 1.5 - \frac{3}{100}Q
    = 1.5 - 0.03\,Q.
  \]

  \textit{Solve:} This is a linear first-order equation. The integrating factor is
  \[
    \mu(t) = e^{\int 0.03\,\mathrm{d}t} = e^{0.03t}.
  \]
  Multiply through:
  \[
    \frac{\mathrm{d}}{\mathrm{d}t}\!\left(Q e^{0.03t}\right)
    = 1.5\,e^{0.03t}.
  \]
  Integrate:
  \[
    Q(t)\,e^{0.03t} = \frac{1.5}{0.03}\,e^{0.03t} + C
    = 50\,e^{0.03t} + C,
  \]
  \[
    Q(t) = 50 + C\,e^{-0.03t}.
  \]

  Apply $Q(0) = 20$: $20 = 50 + C \implies C = -30$.
  \[
    \boxed{Q(t) = 50 - 30\,e^{-0.03t}}.
  \]

  \textit{Long-term behavior:} As $t \to \infty$, $Q(t) \to 50$~kg. This makes physical sense: the equilibrium concentration matches the inflow concentration, so the tank eventually holds $0.5 \times 100 = 50$~kg of salt.

  \textit{Phase line interpretation:} The autonomous equation $Q' = 1.5 - 0.03Q$ has one equilibrium at $Q^* = 50$. Since $f'(Q^*) = -0.03 < 0$, it is asymptotically stable.
\end{workedexample}

\subsubsection{Falling Body with Air Resistance}
\label{sec:ch03_falling_body}

\textbf{Physical setup.} A body of mass $m$ falls under gravity. Air resistance opposes the motion. We model the speed $v(t)$ (positive downward).

\textbf{Model with linear drag.} The drag force is proportional to speed: $F_{\text{drag}} = -kv$. Newton's second law gives
\begin{equation}
  \label{eq:falling_linear}
  m\,\frac{\mathrm{d}v}{\mathrm{d}t} = mg - kv.
\end{equation}
The equilibrium (terminal velocity) is found by setting $v' = 0$:
\[
  mg - kv^* = 0 \implies v^* = \frac{mg}{k}.
\]
Since $f'(v^*) = -k/m < 0$, the terminal velocity is asymptotically stable: any initial speed eventually converges to $v^*$.

\begin{workedexample}
  \textbf{A 10~kg object falls under gravity ($g = 9.8$~m/s\textsuperscript{2}) with linear air resistance ($k = 2$~N$\cdot$s/m). Find the terminal velocity and $v(t)$ if the object is dropped from rest.}

  \textit{Terminal velocity:}
  \[
    v^* = \frac{mg}{k} = \frac{10 \cdot 9.8}{2} = 49 \text{ m/s}.
  \]

  \textit{Solve the ODE:} Divide by $m = 10$:
  \[
    \frac{\mathrm{d}v}{\mathrm{d}t} = 9.8 - \frac{2}{10}v = 9.8 - 0.2\,v.
  \]
  Separate:
  \[
    \frac{\mathrm{d}v}{9.8 - 0.2v} = \mathrm{d}t
    \implies -\frac{1}{0.2}\ln|9.8 - 0.2v| = t + C.
  \]
  With $v(0) = 0$: $-\frac{1}{0.2}\ln(9.8) = C$.
  \[
    \ln|9.8 - 0.2v| = -0.2t + \ln(9.8)
    \implies 9.8 - 0.2v = 9.8\,e^{-0.2t},
  \]
  \[
    \boxed{v(t) = 49\bigl(1 - e^{-0.2t}\bigr)}.
  \]

  \textit{Check:} As $t \to \infty$, $v(t) \to 49$~m/s (terminal velocity). At $t = 5$~s:
  \[
    v(5) = 49(1 - e^{-1}) = 49(1 - 0.368) \approx 31.1 \text{ m/s}.
  \]
\end{workedexample}

\textbf{Model with quadratic drag.} For high-speed motion (e.g., skydiving), the drag force is proportional to $v^2$:
\begin{equation}
  \label{eq:falling_quadratic}
  m\,\frac{\mathrm{d}v}{\mathrm{d}t} = mg - kv^2.
\end{equation}
The terminal velocity is $v^* = \sqrt{mg/k}$. The qualitative behavior is the same---solutions converge to $v^*$ asymptotically---but the approach is different from the linear case.

\subsection{Summary}
\label{sec:ch03_summary}

\begin{table}[htbp]
\centering
\caption{Chapter 3 Summary: Qualitative Analysis and Numerical Methods}
\label{tab:ch03_summary}
\begin{tabular}{l p{5.5cm}}
\toprule
\textbf{Concept} & \textbf{Key Formula/Method} \\
\midrule
Autonomous equation & $\displaystyle \frac{\mathrm{d}y}{\mathrm{d}t} = f(y)$, no explicit $t$-dependence \\
Equilibrium & $y^*$ such that $f(y^*) = 0$ \\
Phase line & Vertical line with equilibria marked and arrows showing flow direction \\
Stability (sink) & $f'(y^*) < 0 \;\Rightarrow$ asymptotically stable \\
Stability (source) & $f'(y^*) > 0 \;\Rightarrow$ unstable \\
Semi-stable (node) & $f'(y^*) = 0$; sign of $f$ determines approach/departure \\
Logistic equation & $\displaystyle \frac{\mathrm{d}y}{\mathrm{d}t} = ry\!\left(1 - \frac{y}{K}\right)$ \\
Logistic solution & $\displaystyle y(t) = \frac{K}{1 + A e^{-rt}}, \;\; A = \frac{K-y_0}{y_0}$ \\
Euler's method & $y_{n+1} = y_n + h\,f(t_n, y_n)$ \\
Euler global error & $O(h)$ \\
Newton's cooling & $\displaystyle T(t) = T_a + (T_0 - T_a)e^{kt}, \; k < 0$ \\
Mixing (constant $V$) & $\displaystyle \frac{\mathrm{d}Q}{\mathrm{d}t} + \frac{r}{V_0}Q = c_1 r$ \\
Linear drag terminal velocity & $v^* = mg/k$ \\
Quadratic drag terminal velocity & $v^* = \sqrt{mg/k}$ \\
\bottomrule
\end{tabular}
\end{table}
