### Solving pure-time differential equations with the Forward-Euler algorithm

\begin{align*} x'(t) &= t^3 -5t - 2\\ x(0) &=10 \end{align*}

Goal: estimate the solution $x(t)$.

What is the initial slope of the function $x(t)$?
$x'(0) = (0)^3-5(0)-2 = -2$

If pretend slope didn't change, would get linear approximation: $L_0(t) = x(0) + x'(0)t = 10 -2t$. $L_\color{red}{0}(t) = x(\color{red}{0}) + x'(\color{red}{0})t = 10 -2t$.

Solution $L_0(t)$ fine if $x'(t)=-2$ always.

But $x'(t)$ changes with time. $L_0(t)$ is OK only for a short time.

\begin{align*} x'(t) &= t^3-5t-2\\ x(0) &= 10 \end{align*}

$L_0(t) = 10-2t$   (OK for $t$ near zero.)

Forward Euler: use $L_0(t)$ for time interval $\Delta t$.
Then find new linear approximation.

Choose $\Delta t = 1$.

Approximate $x(1) \approx L_0(1)=10-2(1)=8$.

Repeat. Linear approximation at $t=1$: $L_\color{\red}{1}(t) = x(\color{red}{1}) + x'(\color{red}{1})(t-\color{red}{1})$

Calculate $x'(1) = (1)^3-5(1)-2 = -6$

For $t > 1$, approximate $x(t) \approx L_1(t) = 8-6(t-1)$

Approximate $x(2) \approx L_1(2) = 8-6(2-1) = 2.$

\begin{align*} x'(t) &= t^3-5t-2\\ x(0) &= 10 \end{align*}

Summarize:

$x(1) \approx L_0(1) = x(0) + x'(0)(1-0) = 8$

$x(2) \approx L_1(2) = x(1) + x'(1)(2-1) = 2$

Continue:
$L_2(t) = x(2) + x'(2)(t-2)$
$x'(2) = (2)^3-5(2)-2 =$ $-4$ $\color{red}{\underline{\color{black}{-4}}}$
$L_2(t) = 2 - 4(t-2)$

$x(3) \approx L_2(3) = 2-4(3-2) = -2$

One more step:
$L_3(t) = x(3) + x'(3)(t-3)$
$x'(3) = (3)^3-5(3)-2 =$ $10$ $\color{red}{\underline{\color{black}{10}}}$
$L_3(t) = -2 +10(t-3)$

$x(4) \approx L_3(4) = -2+10(4-3) = 8$

\begin{align*} x'(t) &= t^3-5t-2\\ x(0) &= 10 \end{align*}

Summarize:

$x(1) \approx L_0(1) = x(0) + x'(0)(1-0) = 8$

$x(2) \approx L_1(2) = x(1) + x'(1)(2-1) = 2$

$x(3) \approx L_2(3) = x(2) + x'(2)(3-2) = -2$

$x(4) \approx L_3(4) = x(3) + x'(3)(4-3) = 8$

$x(1) \approx x(0) + x'(0)(1-0)$

$x(2) \approx x(1) + x'(1)(2-1)$

$x(3) \approx x(2) + x'(2)(3-2)$

$x(4) \approx x(3) + x'(3)(4-3)$

$x(1) \approx x(0) + x'(0)\Delta t$

$x(2) \approx x(1) + x'(1)\Delta t$

$x(3) \approx x(2) + x'(2)\Delta t$

$x(4) \approx x(3) + x'(3)\Delta t$

$x(t_1) \approx x(t_0) + x'(t_0)\Delta t$

$x(t_2) \approx x(t_1) + x'(t_1)\Delta t$

$x(t_3) \approx x(t_2) + x'(t_2)\Delta t$

$x(t_4) \approx x(t_3) + x'(t_3)\Delta t$

Define time points:
\begin{align*} t_0 &= 0\Delta t= 0 & t_1 &=\Delta t=1\\ t_2&=2\Delta t =2 & t_3 &=3\Delta t=3\\ t_4&=4\Delta t=4 \end{align*}
$t_0=0, ~t_1=\Delta t, ~t_2=\Delta t, ~t_3=3\Delta t, \ldots$
Forward Euler formula
$x(t_{i+1}) \approx x(t_i) + x'(t_i)\Delta t$
for $i=0,1,2, \ldots$
Idea of Forward Euler Algorithm
1. Calculate slope from differential equation.
2. Take step of size $\Delta t$ with slope.
3. Repeat.
\begin{align*} x'(t) &= t^3-5t-2\\ x(0) &= 10 \end{align*}

Forward Euler formula
$x(t_{i+1}) \approx x(t_i) + x'(t_i)\Delta t$     for $i=0,1,2, \ldots$

Is our Forward Euler solution accurate?
How improve it? Smaller time step $\Delta t$.

Try $\Delta t = 0.5$. Twice as many steps.

$x'(0)=-2 \Rightarrow x(0.5) \approx 10 -2 \cdot 0.5=9$
$x'(0.5) = -4.375 \Rightarrow x(1) \approx 9 - 4.375(0.5) = 6.8125$
$x'(1) = -6 \Rightarrow x(1.5) \approx 6.8125 - 6(0.5) = 3.8125$
$x'(1.5) = -6.125 \Rightarrow x(2) \approx 3.8125 - 6.125(0.5) = 0.75$
$x'(2) = -4 \Rightarrow x(2.5) \approx 0.75 - 4(0.5) = -1.25$
$x'(2.5) = 1.125 \Rightarrow x(3) \approx -1.25 + 1.125(0.5) = -0.6875$
$x'(3) = 10 \Rightarrow x(3.5) \approx -0.6875 + 10(0.5) = 4.3125$
$x'(3.5) = 23.375 \Rightarrow x(4) \approx 4.3125 + 23.375(0.5) = 16$

$\Delta t=0.5$: more accurate
Computer program: smaller $\Delta t$.
For example: $\Delta t = \frac{1}{4}$ ,   $\Delta t=\frac{1}{8}$