Differential Equations Day 4 -- Intro to Numerical Methods: Euler's Method

Overview
So far we've seen that separable differential equations can be solved with an algebraic process. To review:[br][br][list][*]Be sure the separable differential equation is in standard form [math]N\left(y\right)\cdot\frac{dy}{dx}=M\left(x\right)[/math][br][/*][*]Multiply by [i]dx[/i]  [math]N\left(y\right)\cdot\frac{dy}{dx}\cdot dx=M\left(x\right)\cdot dx\mapsto N\left(y\right)\cdot dy=M\left(x\right)\cdot dx[/math][br][/*][*]Integrate both sides [math]\int N\left(y\right)dy=\int M\left(x\right)dx[/math] (Warning: this can be pretty tricky Calc 1 or 2!)[br][/*][*]Solve the resulting equation for [i]y[/i] to obtain the [b]general solution[/b][/*][*]If an [b]initial condition[/b] [i]y[/i](0)=[i]K[/i] is present, then use it to find the [b]particular solution[/b] that solves the initial condition. In practice this involves using the initial condition to find the constant (or constants) of integration that corresponds to the particular solution that solves the initial condition. (WARNING: this can be pretty tricky algebra 1 and 2!)[/*][/list]An alternate to an algebraic process such as this one is to use a [b]numerical method[/b] to [i]estimate[/i] the solution of a differential equation with an initial condition. [br][br]There are two classes of numerical methods that we will study this semester[br][list=1][*]Euler's (Numerical) Method[/*][*]Runge Kutta (Numerical) Method[/*][/list]The intuitive principle of both numerical methods is actually quite straightforward: use the tick marks of a slope field as a "guide" to "draw" a solution function starting at the initial condition. [br][br]The way they actually work amounts to putting this intuitive principle into mathematical practice.
The Intuitive Principle of Numerical Methods
Consider the following separable differential equation with initial condition:[br][br][math]\frac{dy}{dx}=\sin\left(x\right)\ln\left(y\right);y\left(0\right)=3[/math][br][br]It is in fact separable, and the standard form for separability is[br][br][math]\frac{1}{\ln\left(y\right)}\cdot\frac{dy}{dx}=\sin\left(x\right)[/math][br][br]Multiplying by dx gets it ready for the integral[br][br][math]\frac{1}{\ln\left(y\right)}\cdot\frac{dy}{dx}\cdot dx=\sin\left(x\right)\cdot dx\mapsto\frac{1}{\ln\left(y\right)}dy=\sin\left(x\right)dx[/math][br][br][br]The question now is: how do you integrate 1/ln([i]y[/i])? The answer is: [url=https://socratic.org/questions/how-do-you-integrate-1-ln-x]you don't[/url]. This particular function is known to have no antiderivative in elementary functions. [br][br]Numerical methods come to the rescue here, and also offer a suite of tools when you frankly just don't feel like doing the algebra, and an approximate answer is "good enough."[br][br]The intuitive principle behind numerical methods is that the slope tick marks in the slope field offer a "guide" of how the solution functions move through the [i]x[/i]-[i]y[/i] plane. You simply use these "guides" to build an estimate function.
Find the Pen Tool [icon]/images/ggb/toolbar/mode_pen.png[/icon] in the toolbar (under the Move Tool [icon]/images/ggb/toolbar/mode_move.png[/icon]drop down menu). Starting at the initial condition draw a curve that matches the slope field and that moves to the [i]right only[/i]. It's ok if it isn't perfect.[br][br]Congrats! You've just implemented the intuitive principle of numerical methods for estimating particular solutions of differential equations!
The Mathematical Practice of Numerical Methods: Euler's Method
The numerical method we will learn in this activity is called [b]Euler's (Numerical) Method.[/b] The goal of Euler's Method is to build a piecewise linear function that approximates a particular solution of a differential equation with an initial condition by using the tick marks of the slope field as a "guide". [br][br]This applet below illustrates an [i]n[/i]=5 step Euler's Method approximation with [i]h[/i]=0.5. More on [i]n[/i] and [i]h[/i] in a moment. [br][br]For now, you can adjust [i]h[/i]. You can also adjust the initial condition. You cannot adjust [i]n[/i] in this applet, but that’s only because I didn’t allow it. You’ll be able to adjust [i]n[/i] later when we see how to do the behind the scenes "programming" that lets GeoGebra implement Euler's Method.
Here's how Euler's Method works:[br][br][list=1][*]Put the differential equation in [b]slope field form[/b], [math]\frac{dy}{dx}=f\left(x,y\right)[/math]. All this means in practice is that [math]\frac{dy}{dx}[/math] must be on the left, and everything else on the right. For example, slope field form of the differential equation we've been looking at is [math]\frac{dy}{dx}=\sin\left(x\right)\cdot\ln\left(y\right);y\left(0\right)=3[/math][br][/*][*]Select a [b]step count[/b], [i]n[/i]. This is how many steps of an estimated solution you need. In the example above [i]n[/i]=5.[/*][*]Select a [b]step size[/b] [i]h[/i] (or sometimes also called [i]dx[/i] or [i]dt[/i]). This is how much horizontal ground will be covered in each step. Typical values are close to 0. In the example above [i]h[/i]=0.5.[/*][*]The [b]first estimate point[/b] is the initial condition as a point in the plane. In the example above this is (0,3).[/*][*]Use the [b]Euler's Method formulas[/b] to calculate the [i]x[/i] and [i]y[/i] coordinates of the next estimate point. See below for the formulas.[/*][*][b]Continue[/b] for [i]n[/i] steps. [/*][/list][br] The Euler Method formulas are: [br][br][math]x_{n+1}=x_n+h[/math] [br][math]y_{n+1}=y_n+f\left(x_n,y_n\right)\cdot h[/math][br][br]Note: The function [i]f[/i] in the second equation is the right hand side of the differential equation when in slope field form. [br][br]For instance, to calculate the second estimate point in the example above, the initial condition is the first estimate point [math]\left(x_1,y_1\right)=\left(0,3\right)[/math]. Thus [br][br][math]x_2=x_1+h=0+0.5=0.5[/math] [br][br]and [br][br][math]y_2=y_1+f\left(x_1,y_1\right)\cdot h=3+\left(\sin\left(0\right)\cdot\ln\left(3\right)\right)\cdot0.5=3+\left(0\cdot1.0986...\right)\cdot0.5=3+\left(0\right)\cdot0.5=3[/math][br][br]and so the second estimate point is [math]\left(x_2,y_2\right)=(0.5,3)[/math]. You can see the second estimate point above just right of the initial condition (which is the first estimate point).[br][br]To calculate the third estimate point,[br][br] [math]x_3=x_2+0.5=0.5+0.5=1[/math] [br][br]and [br][br][math]y_3=y_2+f\left(x_2,y_2\right)\cdot h=3+\left(\sin\left(0.5\right)\ln\left(3\right)\right)\cdot0.5[/math][br][br]which is approximately 3.263. Thus the third estimate point is [math]\left(x_3,y_3\right)\approx\left(1,3.263\right)[/math], just to the right and above the second estimate point. Again, you can see it in the applet above. [br][br]To calculate more estimate points simply continue with the calculations as above. It's tedious to do by hand, but it's a perfect task for a computer. Later this semester we'll see how to code Euler's Method and other methods into GeoGebra and Octave (an open source version of MATLAB).
Accuracy vs. Distance: An Issue of Computation
In the applet above illustrating Euler's Method you'll see that each estimate point is separated by a horizontal distance equal to [i]h[/i]. In general: the larger [i]h[/i] is, the less accurate the estimate points are of the actual specific solution. [br][br]On the other hand, the larger [i]h[/i] is, the longer distance the estimate points cover. If you need the estimate of the solution to extend for a long distance (or time) to the right, then you "get more" solution if [i]h[/i] is larger. If you don't want to do much work calculating lots of steps, then you might set h to a fairly large value to get "more bang for your buck."[br][br]A few decades ago when computing was more slow, it was common to have to balance the size of h against the desired rightward distance you wanted the estimated solution to cover. However in modern computing systems, it is generally not a challenge to compute [i]many[/i] steps, and so [i]h[/i] can be kept quite small. [br][br]That said, in estimating solutions to multidimensional partial differential equations (for example numerical weather prediction), it is still a challenge to get enough computing time to estimate solutions for a long period of time. [br][br]Furthermore, when implementing more computationally expensive methods such as Runge Kutta, the amount of computations to implement a numerical method can increase drastically[br][br]I know this sounds overwhelming, but you should be excited! This is a [i]very[/i] active area of mathematical activity to optimize estimating processes and data structures to have the models run quickly. There is also a lot of work to be done simply to maintain and interpret the results of these models There are many careers and job opportunities for people in this area of work. If you're interested: stay tuned![br][br]For now however, let's content ourselves with learning about Euler's Method. The first numerical method is the hardest one to learn! All other methods have some resemblance to Euler's Method in that they all use the slope field of a differential equation to provide information about how to "move through space".

Information: Differential Equations Day 4 -- Intro to Numerical Methods: Euler's Method