\section{First-Order Methods}
\label{ch:first_order}

\usetikzlibrary{positioning}

\subsection{Separable Equations}
\label{sec:ch02_separable}

A first-order ODE is \textbf{separable} when it can be written as
\begin{equation}
  \frac{\diff y}{\diff x} = g(x)\,h(y)
\end{equation}
i.e.\ the right-hand side factors into a function of $x$ times a function of $y$.

\begin{keyresult}
  \textbf{Separable equations.} Rewrite as $\dfrac{\diff y}{h(y)} = g(x)\,\diff x$ and integrate both sides:
  \[
    \int \frac{\diff y}{h(y)} = \int g(x)\,\diff x + C.
  \]
  Solve for $y$ explicitly when possible; otherwise leave the solution in implicit form.
\end{keyresult}

\begin{hintbox}
  If $h(y_0)=0$, the constant function $y(x)=y_0$ is always a solution (an \emph{equilibrium}). These may be lost when dividing by $h(y)$, so check separately.
\end{hintbox}

\begin{workedexample}
  Solve $\dfrac{\diff y}{\diff x} = x\,y$.

  \textbf{Solution.} Separate variables:
  \[
    \frac{\diff y}{y} = x\,\diff x.
  \]
  Integrate:
  \[
    \ln|y| = \frac{x^2}{2} + C.
  \]
  Exponentiate:
  \[
    |y| = e^{C}\,e^{x^2/2} \quad\Longrightarrow\quad y = C_1\,e^{x^2/2},
  \]
  where $C_1 = \pm e^{C}$ is an arbitrary nonzero constant. Including the equilibrium $y=0$ (lost when dividing by $y$), the general solution is
  \[
    y(x) = C\,e^{x^2/2}, \qquad C \in \R.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $\dfrac{\diff y}{\diff x} = \dfrac{2x}{3y^2}$.

  \textbf{Solution.} Separate:
  \[
    3y^2\,\diff y = 2x\,\diff x.
  \]
  Integrate:
  \[
    y^3 = x^2 + C.
  \]
  This implicit form is perfectly acceptable. Solving explicitly gives $y = \sqrt[3]{x^2 + C}$.
\end{workedexample}

\subsection{Linear First-Order Equations}
\label{sec:ch02_linear_first_order}

A first-order ODE is \textbf{linear} if it can be written in the \emph{standard form}
\begin{equation}
  \frac{\diff y}{\diff x} + p(x)\,y = g(x).
  \label{eq:linear_std}
\end{equation}
The coefficient of $\diff y/\diff x$ \textbf{must be $1$}; if the equation arrives with a leading coefficient $a(x)$, divide through first.

\begin{keyresult}
  \textbf{Integrating factor.} For \cref{eq:linear_std}, the integrating factor is
  \[
    \mu(x) = \exp\!\left(\int p(x)\,\diff x\right).
  \]
  Multiplying the equation by $\mu(x)$ produces
  \[
    \frac{\diff}{\diff x}\bigl[\mu(x)\,y\bigr] = \mu(x)\,g(x),
  \]
  so the general solution is
  \[
    y(x) = \frac{1}{\mu(x)}\left(\int \mu(x)\,g(x)\,\diff x + C\right).
  \]
\end{keyresult}

\textbf{Derivation.} With $\mu(x) = \exp\!\bigl(\int p(x)\,\diff x\bigr)$, the chain rule gives
$\mu'(x) = \mu(x)\,p(x)$. Multiplying \cref{eq:linear_std} by $\mu(x)$:
\[
  \mu y' + \mu p\,y = \mu g \quad\Longrightarrow\quad \mu y' + \mu' y = \mu g.
\]
The left side is exactly the product-rule derivative $\dfrac{\diff}{\diff x}[\mu y]$, yielding the result.

\begin{hintbox}
  \textbf{Pitfall.} If the equation is $a(x)y' + b(x)y = f(x)$ with $a(x)\neq 1$, you \emph{must} divide by $a(x)$ to obtain standard form before computing $\mu(x)$. Forgetting this is the most common error.
\end{hintbox}

\begin{workedexample}
  Solve $y' + \dfrac{2}{x}\,y = x^3$.

  \textbf{Solution.} The equation is already in standard form with $p(x)=\dfrac{2}{x}$ and $g(x)=x^3$.
  Compute the integrating factor:
  \[
    \mu(x) = \exp\!\left(\int \frac{2}{x}\,\diff x\right)
            = \exp\!\bigl(2\ln|x|\bigr)
            = |x|^2 = x^2.
  \]
  (We drop the absolute value since $x^2 \ge 0$.)

  Multiply the entire equation by $x^2$:
  \[
    x^2 y' + 2x\,y = x^5
    \quad\Longrightarrow\quad
    \frac{\diff}{\diff x}\bigl[x^2 y\bigr] = x^5.
  \]
  Integrate:
  \[
    x^2 y = \frac{x^6}{6} + C.
  \]
  Divide by $x^2$:
  \[
    y(x) = \frac{x^4}{6} + \frac{C}{x^2}.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $2x\,y' + 3y = 6x$.

  \textbf{Solution.} The leading coefficient is $2x \neq 1$. Divide through:
  \[
    y' + \frac{3}{2x}\,y = 3.
  \]
  Now $p(x) = \dfrac{3}{2x}$ and $g(x) = 3$. Integrating factor:
  \[
    \mu(x) = \exp\!\left(\int \frac{3}{2x}\,\diff x\right)
            = \exp\!\left(\tfrac{3}{2}\ln|x|\right)
            = |x|^{3/2}.
  \]
  Work with $x>0$ for simplicity, so $\mu(x) = x^{3/2}$. Multiply:
  \[
    x^{3/2}y' + \tfrac{3}{2}x^{1/2}y = 3x^{3/2}
    \quad\Longrightarrow\quad
    \frac{\diff}{\diff x}\!\left[x^{3/2}y\right] = 3x^{3/2}.
  \]
  Integrate:
  \[
    x^{3/2}y = \int 3x^{3/2}\,\diff x = 3\cdot\frac{2}{5}x^{5/2} + C = \frac{6}{5}x^{5/2} + C.
  \]
  Solve for $y$:
  \[
    y(x) = \frac{6}{5}x + \frac{C}{x^{3/2}}.
  \]
\end{workedexample}

\subsection{Exact Equations}
\label{sec:ch02_exact}

An ODE written in differential form
\begin{equation}
  M(x,y)\,\diff x + N(x,y)\,\diff y = 0
\end{equation}
is \textbf{exact} if there exists a scalar function $\psi(x,y)$ such that
$\pd{\psi}{x} = M$ and $\pd{\psi}{y} = N$. The solution is then given implicitly by
$\psi(x,y) = C$.

\begin{keyresult}
  \textbf{Exactness test.} The equation is exact \emph{if and only if}
  \[
    \pd{M}{y} = \pd{N}{x}.
  \]
  When exact, the potential function is
  \[
    \psi(x,y) = \int M\,\diff x \;+\; \int\!\left[N - \pd{}{y}\!\left(\int M\,\diff x\right)\right]\!\diff y.
  \]
  The solution is $\psi(x,y) = C$.
\end{keyresult}

\begin{workedexample}
  Solve $(2xy + y^3)\,\diff x + (x^2 + 3xy^2)\,\diff y = 0$.

  \textbf{Solution.} Here $M = 2xy + y^3$ and $N = x^2 + 3xy^2$.
  Check exactness:
  \[
    \pd{M}{y} = 2x + 3y^2, \qquad \pd{N}{x} = 2x + 3y^2.
  \]
  They match, so the equation is exact.

  Integrate $M$ with respect to $x$:
  \[
    \int (2xy + y^3)\,\diff x = x^2y + xy^3 + h(y).
  \]
  Differentiate with respect to $y$ and equate to $N$:
  \[
    \pd{}{y}\bigl[x^2y + xy^3 + h(y)\bigr] = x^2 + 3xy^2 + h'(y) \stackrel{!}{=} x^2 + 3xy^2.
  \]
  Thus $h'(y) = 0$, so $h(y)$ is constant. The potential function is
  \[
    \psi(x,y) = x^2y + xy^3.
  \]
  The implicit solution is
  \[
    x^2y + xy^3 = C.
  \]
\end{workedexample}

\paragraph{Integrating factors for non-exact equations.}
If $\pd{M}{y} \neq \pd{N}{x}$, the equation can sometimes be made exact by multiplying by an integrating factor $\mu$.

\begin{keyresult}
  \textbf{Integrating factors.}
  \begin{itemize}
    \item If $\displaystyle \frac{\pd{M}{y} - \pd{N}{x}}{N}$ depends \emph{only on $x$}, then
          $\mu(x) = \exp\!\left(\int \frac{\pd{M}{y} - \pd{N}{x}}{N}\,\diff x\right)$.
    \item If $\displaystyle \frac{\pd{N}{x} - \pd{M}{y}}{M}$ depends \emph{only on $y$}, then
          $\mu(y) = \exp\!\left(\int \frac{\pd{N}{x} - \pd{M}{y}}{M}\,\diff y\right)$.
  \end{itemize}
\end{keyresult}

\begin{workedexample}
  Solve $(3xy + y^2)\,\diff x + (x^2 + xy)\,\diff y = 0$.

  \textbf{Solution.} $M = 3xy + y^2$, $N = x^2 + xy$. Check:
  \[
    \pd{M}{y} = 3x + 2y, \qquad \pd{N}{x} = 2x + y.
  \]
  Not exact. Test for an integrating factor depending on $x$ alone:
  \[
    \frac{\pd{M}{y} - \pd{N}{x}}{N}
    = \frac{(3x+2y) - (2x+y)}{x^2 + xy}
    = \frac{x + y}{x(x+y)}
    = \frac{1}{x}.
  \]
  This depends only on $x$, so
  \[
    \mu(x) = \exp\!\left(\int \frac{1}{x}\,\diff x\right) = \exp(\ln|x|) = x.
  \]
  Multiply the original equation by $\mu = x$:
  \[
    (3x^2y + xy^2)\,\diff x + (x^3 + x^2y)\,\diff y = 0.
  \]
  Verify: $\pd{\tilde{M}}{y} = 3x^2 + 2xy = \pd{\tilde{N}}{x}$. Exact.

  Integrate $\tilde{M}$ with respect to $x$:
  \[
    \int (3x^2y + xy^2)\,\diff x = x^3y + \tfrac{1}{2}x^2y^2 + h(y).
  \]
  Differentiate with respect to $y$:
  \[
    x^3 + x^2y + h'(y) \stackrel{!}{=} x^3 + x^2y \quad\Longrightarrow\quad h'(y)=0.
  \]
  The potential is $\psi(x,y) = x^3y + \dfrac{1}{2}x^2y^2$, and the solution is
  \[
    x^3y + \tfrac{1}{2}x^2y^2 = C.
  \]
\end{workedexample}

\subsection{Bernoulli Equations}
\label{sec:ch02_bernoulli}

A \textbf{Bernoulli equation} has the form
\begin{equation}
  \frac{\diff y}{\diff x} + p(x)\,y = g(x)\,y^n, \qquad n \neq 0,1.
\end{equation}
For $n=0$ the equation is linear; for $n=1$ it is also linear. The substitution $v = y^{1-n}$ transforms it into a linear equation.

\begin{keyresult}
  \textbf{Bernoulli substitution.} Let $v = y^{1-n}$. Then
  \[
    v' = (1-n)\,y^{-n}\,y',
  \]
  and substituting into the Bernoulli equation yields the \emph{linear} equation in $v$:
  \[
    v' + (1-n)\,p(x)\,v = (1-n)\,g(x).
  \]
\end{keyresult}

\begin{workedexample}
  Solve $y' + \dfrac{1}{x}\,y = x\,y^3$.

  \textbf{Solution.} This is Bernoulli with $p(x)=\dfrac{1}{x}$, $g(x)=x$, and $n=3$.
  Substitute $v = y^{1-3} = y^{-2}$. Then $v' = -2y^{-3}y'$.

  Multiply the original equation by $-2y^{-3}$:
  \[
    -2y^{-3}y' - 2y^{-3}\cdot\frac{1}{x}\,y = -2y^{-3}\cdot x\,y^3.
  \]
  Recognizing $v = y^{-2}$ and $v' = -2y^{-3}y'$:
  \[
    v' - \frac{2}{x}\,v = -2x.
  \]
  This is a linear equation in $v$. Integrating factor:
  \[
    \mu(x) = \exp\!\left(\int -\frac{2}{x}\,\diff x\right) = \exp(-2\ln|x|) = \frac{1}{x^2}.
  \]
  Multiply by $\mu$:
  \[
    \frac{1}{x^2}v' - \frac{2}{x^3}v = -\frac{2}{x}
    \quad\Longrightarrow\quad
    \frac{\diff}{\diff x}\!\left[\frac{v}{x^2}\right] = -\frac{2}{x}.
  \]
  Integrate:
  \[
    \frac{v}{x^2} = -2\ln|x| + C
    \quad\Longrightarrow\quad
    v = x^2\bigl(C - 2\ln|x|\bigr).
  \]
  Substitute back $v = y^{-2}$:
  \[
    \frac{1}{y^2} = x^2\bigl(C - 2\ln|x|\bigr)
    \quad\Longrightarrow\quad
    y(x) = \pm\frac{1}{x\sqrt{\,C - 2\ln|x|\,}}.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $y' - \dfrac{2}{x}\,y = x^2\,y^2$.

  \textbf{Solution.} Bernoulli with $n=2$, $p(x)=-\dfrac{2}{x}$, $g(x)=x^2$.
  Substitute $v = y^{1-2} = y^{-1} = \dfrac{1}{y}$. Then $v' = -y^{-2}y'$.

  Multiply the original equation by $-y^{-2}$:
  \[
    -y^{-2}y' + \frac{2}{x}\,y^{-1} = -x^2.
  \]
  In terms of $v$:
  \[
    v' + \frac{2}{x}\,v = -x^2.
  \]
  Integrating factor:
  \[
    \mu(x) = \exp\!\left(\int \frac{2}{x}\,\diff x\right) = x^2.
  \]
  Multiply:
  \[
    x^2v' + 2xv = -x^4
    \quad\Longrightarrow\quad
    \frac{\diff}{\diff x}\!\left[x^2v\right] = -x^4.
  \]
  Integrate:
  \[
    x^2v = -\frac{x^5}{5} + C
    \quad\Longrightarrow\quad
    v = -\frac{x^3}{5} + \frac{C}{x^2}.
  \]
  Since $v = 1/y$:
  \[
    y(x) = \frac{1}{-\dfrac{x^3}{5} + \dfrac{C}{x^2}}
          = \frac{5x^2}{C - x^5}.
  \]
\end{workedexample}

\subsection{Homogeneous Substitutions}
\label{sec:ch02_homogeneous_substitutions}

A first-order ODE is \textbf{homogeneous} (of degree zero) if
\begin{equation}
  \frac{\diff y}{\diff x} = F\!\left(\frac{y}{x}\right).
\end{equation}
That is, the right-hand side is a function of the ratio $y/x$ alone.

\begin{keyresult}
  \textbf{Homogeneous substitution.} Let $y = vx$. Then
  \[
    \frac{\diff y}{\diff x} = v + x\frac{\diff v}{\diff x}.
  \]
  The equation becomes
  \[
    v + x\frac{\diff v}{\diff x} = F(v)
    \quad\Longrightarrow\quad
    \frac{\diff v}{\diff x} = \frac{F(v) - v}{x},
  \]
  which is separable:
  \[
    \int \frac{\diff v}{F(v) - v} = \int \frac{\diff x}{x} = \ln|x| + C.
  \]
\end{keyresult}

\begin{workedexample}
  Solve $y' = \dfrac{x+y}{x-y}$.

  \textbf{Solution.} Rewrite the right side:
  \[
    \frac{x+y}{x-y} = \frac{1 + y/x}{1 - y/x} = F\!\left(\frac{y}{x}\right).
  \]
  The equation is homogeneous. Set $y = vx$, so $y' = v + xv'$:
  \[
    v + x\frac{\diff v}{\diff x} = \frac{1+v}{1-v}.
  \]
  Isolate the derivative:
  \[
    x\frac{\diff v}{\diff x} = \frac{1+v}{1-v} - v = \frac{1+v - v(1-v)}{1-v} = \frac{1+v^2}{1-v}.
  \]
  Separate:
  \[
    \frac{1-v}{1+v^2}\,\diff v = \frac{\diff x}{x}.
  \]
  Split the left integrand:
  \[
    \int \frac{1}{1+v^2}\,\diff v - \int \frac{v}{1+v^2}\,\diff v = \int \frac{\diff x}{x}.
  \]
  These are standard integrals:
  \[
    \arctan(v) - \tfrac{1}{2}\ln(1+v^2) = \ln|x| + C.
  \]
  Substitute back $v = y/x$:
  \[
    \arctan\!\left(\frac{y}{x}\right) - \tfrac{1}{2}\ln\!\left(1 + \frac{y^2}{x^2}\right) = \ln|x| + C.
  \]
  Using $\ln(1 + y^2/x^2) = \ln\!\bigl((x^2+y^2)/x^2\bigr) = \ln(x^2+y^2) - 2\ln|x|$, the solution simplifies to
  \[
    \arctan\!\left(\frac{y}{x}\right) - \tfrac{1}{2}\ln(x^2+y^2) = C.
  \]
\end{workedexample}

\begin{workedexample}
  Solve $y' = \dfrac{xy + y^2}{x^2}$.

  \textbf{Solution.} Rewrite:
  \[
    y' = \frac{y}{x} + \frac{y^2}{x^2} = F\!\left(\frac{y}{x}\right).
  \]
  Homogeneous with $F(v) = v + v^2$. Set $y = vx$:
  \[
    v + x\frac{\diff v}{\diff x} = v + v^2
    \quad\Longrightarrow\quad
    x\frac{\diff v}{\diff x} = v^2.
  \]
  Separate:
  \[
    \frac{\diff v}{v^2} = \frac{\diff x}{x}.
  \]
  Integrate:
  \[
    -\frac{1}{v} = \ln|x| + C
    \quad\Longrightarrow\quad
    v = -\frac{1}{\ln|x| + C}.
  \]
  Substitute back $v = y/x$:
  \[
    y(x) = -\frac{x}{\ln|x| + C}.
  \]
  The equilibrium solution $y=0$ (corresponding to $v=0$) is also valid.
\end{workedexample}

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

The following flowchart guides method selection. Test methods in the order shown; the first applicable method solves the equation.

\begin{center}
\begin{tikzpicture}[node distance=1.3cm and 1.2cm,
  >=Stealth,
  box/.style={draw, rounded corners=3pt, fill=LightSteelBlue!20, minimum width=3cm, align=center, font=\small, text width=3.2cm},
  sol/.style={draw, rounded corners=3pt, fill=ForestGreen!15, minimum width=3cm, align=center, font=\small, text width=3.0cm},
  arrow/.style={->, >=Stealth, thick, shorten <=1pt, shorten >=1pt}]

  \node[box, fill=LemonChiffon!40] (start) {\textbf{Given:} $y' = f(x,y)$};
  \node[box, below=of start] (t1) {\textbf{Can $f$ be written}\\as $g(x)\,h(y)$?};
  \node[box, below=of t1] (t2) {\textbf{Is it linear}\\$y'+p(x)y=g(x)$?};
  \node[box, below=of t2] (t3) {\textbf{Is it Bernoulli}\\$y'+py=gy^n$?};
  \node[box, below=of t3] (t4) {\textbf{Is $f(x,y)=F(y/x)$?}\\(Homogeneous)};
  \node[box, below=of t4] (t5) {\textbf{Write as $M\,dx+N\,dy=0$.}\\Exact?};
  \node[box, below=of t5, fill=Salmon!20] (gen) {\textbf{Non-exact:}\\Try integrating\\factor or other methods};

  % Solution boxes
  \node[sol, right=2cm of t1] (s1) {Separable:\\Integrate};
  \node[sol, right=2cm of t2] (s2) {Integrating factor:\\$\mu=e^{\int p\,dx}$};
  \node[sol, right=2cm of t3] (s3) {Substitute\\$v=y^{1-n}$};
  \node[sol, right=2cm of t4] (s4) {Substitute\\$y=vx$};
  \node[sol, right=2cm of t5] (s5) {Potential\\$\psi(x,y)=C$};

  % Decision arrows
  \draw[arrow] (start) -- (t1);
  \draw[arrow] (t1) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {No} (t2);
  \draw[arrow] (t2) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {No} (t3);
  \draw[arrow] (t3) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {No} (t4);
  \draw[arrow] (t4) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {No} (t5);
  \draw[arrow] (t5) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {No} (gen);

  % Yes arrows
  \draw[arrow] (t1) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {Yes} (s1);
  \draw[arrow] (t2) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {Yes} (s2);
  \draw[arrow] (t3) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {Yes} (s3);
  \draw[arrow] (t4) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {Yes} (s4);
  \draw[arrow] (t5) -- node[above, font=\footnotesize, fill=white, inner sep=1pt] {Yes} (s5);
\end{tikzpicture}
\end{center}

\begin{table}[htbp]
\centering
\caption{First-order solution methods}
\label{tab:ch02_summary}
\begin{tabular}{l l p{6cm}}
\toprule
\textbf{Type} & \textbf{Form} & \textbf{Method} \\
\midrule
Separable & $y' = g(x)h(y)$ & Rewrite as $\dfrac{\diff y}{h(y)} = g(x)\,\diff x$; integrate both sides \\
Linear & $y' + p(x)y = g(x)$ & Integrating factor $\mu(x) = \exp\!\left(\int p(x)\,\diff x\right)$ \\
Exact & $M\,\diff x + N\,\diff y = 0,\;\pd{M}{y}=\pd{N}{x}$ & Find potential $\psi$ with $\pd{\psi}{x}=M,\;\pd{\psi}{y}=N$; $\psi=C$ \\
Non-exact & $M\,\diff x + N\,\diff y = 0,\;\pd{M}{y}\neq\pd{N}{x}$ & Multiply by integrating factor $\mu(x)$ or $\mu(y)$ \\
Bernoulli & $y' + p(x)y = g(x)y^n$ & Substitute $v=y^{1-n}$; solve resulting linear equation \\
Homogeneous & $y' = F(y/x)$ & Substitute $y=vx$; solve resulting separable equation \\
\bottomrule
\end{tabular}
\end{table}
