---
title: "Improved Euler's method"
author: "Dr. Paul Pearson"
date: "2025-01-31"
output: slidy_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## (Regular/old) Euler's method

One Euler step (blue, dashed) is linear approximation, so it doesn't approximate solutions (black) that curve very well.

![One Euler step.](3_2_Improved_Eulers_method_screenshot_1.png)


## (New) Improved Euler's method

Taking two or more Euler steps will fit a curved solution better, but that's not a new idea.  But, taking two (dashed) Euler steps and using the midpoint (of the line segment connecting the starting and ending points) as the next approximation is a new idea, and this midpoint is usually a better approximation to solutions that curve than one (dashed) Euler step is.  In essence, using the average slopes of two (dashed "phantom") Euler steps does better at approximating solutions that curve than one Euler step does for one time step.


![Two "phantom" Euler steps and their midpoint.](3_2_Improved_Eulers_method_screenshot_2.png)


<!--
The intuitive idea is that by evaluating $f(t,u)$ at both $t = t_k$ and $t = t_{k+1}$ with suitable choices for $u$ and then averaging, the resulting slope $m$ in is more representative of the behavior of the solution over the interval $t_k \leq t \leq t_{k+1}$ than $f(t_k, u_k)$ alone.
-->


## Improved Euler's method: key pieces (without coordinates)

Suppose we know the time increment $h$, and slopes $m_1$, and $m_2$.  Find the vertical segments $v_1$, $v_2$, and find the slope $m$.  Show that $m$ is the average of $m_1$ and $m_2$.

```{r, echo=FALSE, engine = 'tikz', fig.ext = 'svg', fig.align = 'center', out.height='400px'}
\begin{tikzpicture}[scale=2]

\draw (0,0) -- node [midway] {$m_1$} 
(1,0.5) -- node [midway] {$m_2$} 
(2,2);

\draw (0,0) -- node [below] {$h$} 
(1,0) -- node [right] {$v_1$} 
(1,0.5) -- node [below] {$h$} 
(2,0.5) -- node [right] {$v_2$} 
(2,2);

\draw (0,0) -- node [midway, above] {$m$} (2,2);

%\draw [fill = black] (1,1) circle [radius=0.03];

\end{tikzpicture}
```


## Improved Euler's method: key pieces (with coordinates)

Suppose we're given a first order differential equation $u'(t) = f(t,u)$, and we choose a time increment $h$.  To do one step of the improved Euler method:

- Initialize the process at $(t_k, u_k)$.
- Find the slope $m_1$ using $u' = f$.
- Find the "regular/old" Euler approximation $w$ using $m_1$.
- Find the slope $m_2$ using $u' = f$ and $w$.
- Find the slope $m$.
- Find the improved Euler estimate $u_{k+1}$ using the the initial point and $m$.
- Note: $u_{k+1}$ is a numerical estimate for the value $u(t_{k+1})$ of the analytic solution curve.

```{r, echo=FALSE, engine = 'tikz', fig.ext = 'svg', fig.align = 'center', out.height='400px'}
\begin{tikzpicture}[scale=2]

\draw [fill=blue!25] (0,0) -- (1,0) -- (1,1) -- cycle;


\draw (0,0) -- node [midway] {$m_1$} 
(1,0.5) -- node [midway] {$m_2$} 
(2,2);

\draw (0,0) -- node [below] {$h$} 
(1,0) -- node [right] {$v_1$} 
(1,0.5) -- node [below] {$h$} 
(2,0.5) -- node [right] {$v_2$} 
(2,2);

\draw (0,0) -- node [midway, above] {$m$} (2,2);

%\draw [fill = black] (1,1) circle [radius=0.03];

\draw (0,-0.5) node {$t_k$};
\draw (1,-0.5) node {$t_{k+1}$};
\draw (2,-0.5) node {$t_{k+2}$};

\draw (-0.5,0) node {$u_k$};
\draw (-0.5,0.5) node {$w$};
\draw (-0.5,1) node {$u_{k+1}$};
\draw (-0.5,2) node {$\tilde{w}$};


\end{tikzpicture}
```


### Check your work

- Initial point: $(t_k, u_k)$.
- First slope: $m_1 = f(t_k,u_k)$.
- First "regular/old" Euler estimate: $w = u_k + h m_1 = u_k + h f(t_k,u_k)$.
- Second slope: $m_2 = f(t_{k+1},w) = f(t_k + h, w)$.
- Average slope: $m = (m_1 + m_2) / 2 = (f(t_k,u_k) + f(t_k + h, w)) / 2$.
- Improved Euler estimate: $u_{k+1} = u_k + h m$ or $$u_{k+1} = u_k + h \cdot \left( \frac{f(t_k,u_k) + f(t_k + h, w)}{2} \right).$$
