[next] [prev] [prev-tail] [tail] [up]
Note: 1 lecture, §2.4 in [EP], §8.1 in [BD]
As we mentioned before, unless is of a special form, it is generally very hard if not impossible to get a nice formula for the solution of the problem
If the equation can be solved in closed form, we should do that. But what if we have an equation that cannot be solved in closed form? What if we want to ﬁnd the value of the solution at some particular ? Or perhaps we want to produce a graph of the solution to inspect the behavior. In this section we will learn about the basics of numerical approximation of solutions.
The simplest method for approximating a solution is Euler’s method4. It works as follows: We take and compute the slope . The slope is the change in per unit change in . We follow the line for an interval of length on the axis. Hence if at , then we will say that (the approximate value of at ) will be . Rinse, repeat! That is, compute and using and . For an example of the ﬁrst two steps of the method see Figure 1.13.
More abstractly, for any , we compute
The line segments we get are an approximate graph of the solution. Generally it is not exactly the solution. See Figure 1.14 for the plot of the real solution and the approximation.
Let us see what happens with the equation , . Let us try to approximate using Euler’s method. In Figures 1.13 and 1.14 we have graphically approximated with step size 1. With step size 1 we have . The real answer is 3. So we are approximately 1.074 oﬀ. Let us halve the step size. Computing with , we ﬁnd that , so an error of about 0.791. Table 1.1 gives the values computed for various parameters.
The diﬀerence between the actual solution and the approximate solution is called the error. We usually talk about just the size of the error and we do not care much about its sign. The point is, we usually do not know the real solution, so we only have a vague understanding of the error. If we knew the error exactly …what is the point of doing the approximation?
We notice that except for the ﬁrst few times, every time we halved the interval the error approximately halved. This halving of the error is a general feature of Euler’s method as it is a ﬁrst order method. There exists an improved Euler method, see the exercises, that is a second order method reduces the error to approximately one quarter every time we halve the interval. The meaning of “second” order is the squaring in .
To get the error to be within 0.1 of the answer we had to already do 64 steps. To get it to within 0.01 we would have to halve another three or four times, meaning doing 512 to 1024 steps. That is quite a bit to do by hand. The improved Euler method from the exercises should quarter the error every time we halve the interval, so we would have to approximately do half as many “halvings” to get the same error. This reduction can be a big deal. With 10 halvings (starting at ) we have 1024 steps, whereas with 5 halvings we only have to do 32 steps, assuming that the error was comparable to start with. A computer may not care about this diﬀerence for a problem this simple, but suppose each step would take a second to compute (the function may be substantially more diﬃcult to compute than ). Then the diﬀerence is 32 seconds versus about 17 minutes. We are not being altogether fair, a second order method would probably double the time to do each step. Even so, it is 1 minute versus 17 minutes. Next, suppose that we have to repeat such a calculation for diﬀerent parameters a thousand times. You get the idea.
Note that in practice we do not know how large the error is! How do we know what is the right step size? Well, essentially we keep halving the interval, and if we are lucky, we can estimate the error from a few of these calculations and the assumption that the error goes down by a factor of one half each time (if we are using standard Euler).
Exercise 1.7.2: In the table above, suppose you do not know the error. Take the approximate values of the function in the last two lines, assume that the error goes down by a factor of 2. Can you estimate the error in the last time from this? Does it (approximately) agree with the table? Now do it for the ﬁrst two rows. Does this agree with the table?
Let us talk a little bit more about the example , . Suppose that instead of the value we wish to ﬁnd . The results of this eﬀort are listed in Table 1.2 for successive halvings of . What is going on here? Well, you should solve the equation exactly and you will notice that the solution does not exist at . In fact, the solution goes to inﬁnity when you approach .
Another case where things go bad is if the solution oscillates wildly near some point. The solution may exist at all points, but even a much better numerical method than Euler would need an insanely small step size to approximate the solution with reasonable precision. And computers might not be able to easily handle such a small step size.
In real applications we would not use a simple method such as Euler’s. The simplest method that would probably be used in a real application is the standard Runge-Kutta method (see exercises). That is a fourth order method, meaning that if we halve the interval, the error generally goes down by a factor of 16 (it is fourth order as ).
Choosing the right method to use and the right step size can be very tricky. There are several competing factors to consider.
We have seen just the beginnings of the challenges that appear in real applications. Numerical approximation of solutions to diﬀerential equations is an active research area for engineers and mathematicians. For example, the general purpose method used for the ODE solver in Matlab and Octave (as of this writing) is a method that appeared in the literature only in the 1980s.
Exercise 1.7.4: Consider , . a) Use Euler’s method with step sizes to approximate . b) Solve the equation exactly. c) Describe what happens to the errors for each you used. That is, ﬁnd the factor by which the error changed each time you halved the interval.
Exercise 1.7.6: Example of numerical instability: Take , . We know that the solution should decay to zero as grows. Using Euler’s method, start with and compute to try to approximate . What happened? Now halve the interval. Keep halving the interval and approximating until the numbers you are getting start to stabilize (that is, until they start going towards zero). Note: You might want to use a calculator.
The simplest method used in practice is the Runge-Kutta method. Consider , , and a step size . Everything is the same as in Euler’s method, except the computation of and .
There is a simple way to improve Euler’s method to make it a second order method by doing just one extra step. Consider , , and a step size . What we do is to pretend we compute the next step as in Euler, that is, we start with , we compute a slope , and then look at the point . Instead of letting our new point be , we compute the slope at that point, call it , and then take the average of and , hoping that the average is going to be closer to the actual slope on the interval from to . And we are correct, if we halve the step, the error should go down by a factor of . To summarize, the setup is the same as for regular Euler, except the computation of and .
Exercise 1.7.104: Consider , . a) Use the improved Euler’s method with step sizes and to approximate . b) Use Euler’s method with and . c) Solve exactly, ﬁnd the exact value of . d) Compute the errors, and the factors by which the errors changed.