Euler's Method

Computation
All steps
\(n\)\(x_n\)\(y_n\)\(h\)\(f(x_n, y_n)\)\(y_{n+1}\)error
Function f(x, y)
Custom
Use LaTeX with variables x and y (e.g. y\sin(x), x^2+y, e^{-x}y).

Step Parameters
Steps \(N\) = 10
Current step \(n\) = 0
Endpoint \(x_N\) = 4.2

Display
Visual Parameters
Arrow length = 0.40
Field opacity = 0.33
Grid points \(N_x\) = 25
Grid spacing \(d_x\) = 0.50
Many first order differential equations can't be solved by hand. For example, the Existence and Uniqueness Theorem guarantees that $y' = \ln(1 + x^2 + y^2)$ with $y(x_0) = y_0$ has a unique solution, but that solution can't be written in terms of simple functions. Similarly, $y' = e^{-x^2}$ is solved by an antiderivative of $e^{-x^2}$, which has no elementary formula. We know the solution exists, so how do we compute it? The simplest answer is \textbf{Euler's method}, which you may recognize from its mention in "The Imitation Game." The idea starts with the \textbf{slope field}. If a solution of $y' = f(x, y)$ passes through a point $(x_1, y_1)$, then its slope there is exactly $f(x_1, y_1)$. So even with no formula for the solution, the differential equation tells us which direction the solution is heading at every point of the plane. These are the faint line segments in the background of the graph. \begin{algorithm}[Euler's Method] Given the initial value problem $y' = f(x, y)$, $y(x_0) = y_0$ and a step size $h > 0$, let $x_n = x_0 + nh$ and for $n = 0, 1, 2, \dots$ set $$y_{n+1} = y_n + hf(x_n, y_n).$$ Then $y_n \approx y(x_n)$, the true solution at $x_n$. \end{algorithm} Where does this come from? For small $h$, the definition of the derivative gives $$y'(x) \approx \frac{y(x+h) - y(x)}{h},$$ and solving for $y(x+h)$, $$y(x+h) \approx y(x) + hy'(x) = y(x) + hf(x, y(x)),$$ using that $y$ solves the differential equation. We then replace the unknown $y(x_n)$ with our best guess so far, $y_n$. Geometrically, we are simply marching along the slope field. From the current point $(x_n, y_n)$ we follow the line with slope $f(x_n, y_n)$ for a horizontal distance $h$, which is a vertical distance $hf(x_n, y_n)$. Then we stop, read a new slope, and repeat. In the graph, the orange circle is $(x_n, y_n)$, the red line is the slope there, and the dotted legs show the horizontal distance $h$ and vertical distance $hf(x_n, y_n)$ leading to the next point. Move the slider for $n$, or click a row of the table in the \emph{Computation} dropdown under the graph, to march along the blue path, and drag the black starting point to change the initial condition. \begin{remark} Toggle on \emph{True solution} to see it as the green curve (to good accuracy, as it is computed by a much more careful numerical method). Euler's path leaves it for two reasons. First, we follow a straight line for the whole step, while the true solution curves away. Second, after the first step, we read the slope at $(x_n, y_n)$ rather than on the true solution, so we follow slightly the wrong direction, and these errors accumulate from step to step. \end{remark} To reduce the error, reduce the step size, so that we correct course more often. Increase $N$ in the graph, which sets $h = (x_N - x_0)/N$, and watch the blue path close in on the true solution. Doubling $N$ roughly halves the error, once $h$ is small: the error at a fixed $x$ is proportional to $h$. This makes Euler's method a \textit{first order} method, and is why more careful methods, such as the second order Runge Kutta methods, are used in practice. \begin{remark} Two choices of $f$ are worth exploring, as here we can write the iterates exactly. \begin{itemize} \item For $y' = y$, Euler's method gives $y_{n+1} = (1 + h)y_n$, so $y_n = (1+h)^n y_0$. Starting from $y(0) = 1$ with $N$ steps to $x_N = 1$, this approximates $y(1) = e$ by $\left(1 + \tfrac{1}{N}\right)^N$. \item For $y' = -y$, Euler's method gives $y_n = (1-h)^n y_0$. The true solution decays, but when $h > 2$ we have $|1 - h| > 1$ and the iterates oscillate and grow. Try $N = 2$ or $N = 3$. A convergent method can still behave badly when the step is too large. \end{itemize} \end{remark}