To understand that numerical algorithms such as Euler’s method allow the approximation of solutions to the initial value problems and that there are more efficient algorithms than Euler’s method such as those algorithms that use the Runge-Kutta methods.
Just as numerical algorithms are useful when finding the roots of polynomials, numerical methods will prove very useful in our study of ordinary differential equations. Consider the polynomial . We do not need a numerical algorithm to see that the roots of this polynomial are and . However, a numerical method such as the Newton-Raphson Algorithm is very useful for approximating as a decimal. 1
See any calculus text for a description of the Newton-Raphson Algorithm.
Similarly, it may be easier to generate a numerical solution for differential equations if our goal is simply to plot a solution. In addition, there will be differential equations for which it is impossible to find a solution in terms of elementary functions such as polynomials, trigonometric functions, and exponential functions.
Suppose that we wish to solve the initial value problem
(1.4.1)(1.4.2)
The equation is not separable, which currently is the only analytic technique at our disposal. However, we can try to find a numerical approximation for the solution. A numerical approximation is simply a table (possibly very large) of and values.
We will attempt to find a numerical solution for (1.4.1)–(1.4.2) on the interval . Even with the use of a computer, we cannot approximate the solution at every single point on an interval. For the initial value problem
we might be able to find approximations at in at best. If we choose to be equally spaced on , we can write
where and . We say that is the step size for our approximation.
Given an approximation for the solution , the question is how to find an approximate solution at . To generate the second approximation, we will construct a tangent line to the solution at . If we use the slope of the solution curve at , then
Making use of the fact that
or equivalently
the estimate for our solution at is
Similarly, the approximation at will be
Our general algorithm is
The idea is to compute tangent lines at each step and use this information to get our next approximation.
The algorithm that we have described is known as Euler’s method. Let us estimate a solution to (1.4.1)–(1.4.2) on the interval with step size . Since , we can make our first approximation exact,
To generate the second approximation, we will construct a tangent line to the solution at . If we use the slope of the solution curve at ,
The initial value problem (1.4.1)–(1.4.2) is, in fact, solvable analytically with solution . We can compare our approximation to the exact solution in Table 1.4.1. We can also see graphs of the approximate and exact solutions in Figure 1.4.2. Notice that the error grows as we get further away from our initial value. In fact, the graph of the approximation for is obscured by the graph of the exact solution. In addition, a smaller step size gives us a more accurate approximation (Table 1.4.3).
Compare the exact values of the solution (Task 1.4.1.b) to the approximate values of the solution (Task 1.4.1.c) and comment on what happens as varies from to .
The simplest approximation can be obtained by taking the first two terms of the Taylor series. That is, we will use a linear approximation,
This gives us Euler’s method,
The terms that we are omitting, all contain powers of of at least degree two. If is small, then for will be very small and these terms will not matter much.
The condition that there exists a constant such that
whenever and are in is called a Lipschitz condition. Many of the functions that we will consider satisfy such a condition. If the condition is satisfied, we can usually say a great deal about the function.
If we wish to improve upon Euler’s method, we could add more terms of Taylor series. For example, we can obtain a more accurate approximation by using a quadratic Taylor polynomial,
However, we need to know in order to use this approximation. Using the chain rule from multivariable calculus, we can differentiate both sides of to obtain
The problem is that some preliminary analytic work must be done. That is, before we can write a program to compute our solution, we must find and , although this is less of a problem with the availability of computer algebra systems such as Sage.
Around 1900, two German mathematicians, Carle Runge and Martin Kutta, independently invented several numerical algorithms to solve differential equations. These methods, known as Runge-Kutta methods, estimate the higher-order terms of the Taylor series to find an approximation that does not depend on computing derivatives of .
by the Fundamental Theorem of Calculus. In Euler’s method, we approximate the right-hand side of (1.4.3) by
In terms of the definite integral, this is simply a left-hand sum. In the improved Euler’s method or the second-order Runge-Kutta method we will estimate the right-hand side of (1.4.3) using the trapezoid rule from calculus,
Thus, our algorithm becomes
(1.4.4)
However, we have a problem since appears in the right-hand side of our approximation. To get around this difficulty, we will replace in the right-hand side of (1.4.4) with the Euler approximation for . Thus,
To understand that the second-order Runge-Kutta method is actually an improvement over the traditional Euler’s method, we will need to use the Taylor approximation for a function of two variables. Let us assume that is defined on some rectangle and that all of the derivatives of are continuously differentiable. Then
As in the case of the single variable Taylor series, we can write a Taylor polynomial if the Taylor series is truncated,
where the second term is the remainder term and lies on the line segment joining and .
If we are more careful about choosing our parameters, we can obtain agreement up through the term. If we use the two variable Taylor series to expand , we have
where means that of the subsequent terms have a factor of with . Using this expression, we obtain a new form for (1.4.5),
(1.4.7)
Since by the chain rule, we can rewrite (1.4.6) as
(1.4.8)
We can make equations (1.4.7) and (1.4.8) agree up through the quadratic terms if we require that
If we choose and , these equations are satisfied, and we obtain the improved Euler’s method
The improved Euler’s method or the second-order Runge-Kutta method is a more sophisticated algorithm that is less prone to error due to the step size . Euler’s method is based on truncating the Taylor series after the linear term. Since
we know that the error depends on . On the other hand, the error for the improved Euler’s method depends on , since
If we use Simpson’s rule to estimate the integral in
we can improve our accuracy up to . The idea is exactly the same, but the algebra becomes much more tedious. This method is known as the Runge-Kutta method of order 4 and is given by
Error analysis rate of convergence is very important for any numerical algorithm. Our approximation is more accurate for smaller values of . Under reasonable conditions we can also bound the error by
where is the unique solution to the initial value problem
Using Taylor series, we can develop better numerical algorithms to compute solutions of differential equations. The Runge-Kutta methods are an important class of these algorithms.
Use Euler’s method with step size to approximate the solution to the initial value problem on the interval Your solution should include a table of approximate values of the dependent variable as well as the exact values of the dependent variable. Make sure that your approximations are accurate to four decimal places.
Use the error bound theorem (Theorem 1.4.5) to estimate the error at each approximation. Your solution should include a table of approximate values of the dependent variable the exact values of the dependent variable, the error estimates, and the actual error. Make sure that your approximations are accurate to four decimal places.
Subsection1.4.7Sage—Numerical Routines for solving ODEs
Not all differential equations can be solved using algebra and calculus even if we are very clever. If we encounter an equation that we cannot solve or use Sage to solve, we must resort to numerical algorithms like Euler’s method or one of the Runge-Kutta methods, which are best implemented using a computer. Fortunately, Sage has some very good numerical solvers. Sage will need to know the following to solve a differential equation:
x,y=PolynomialRing(RR, 2, "xy").gens() #declare x, y as generators of a polynomial ring
2
eulers_method(x+y, 0,1, 0.5, 5)
Messages
The syntax of eulers_method for the inital value problem
with step size on the interval is eulers_method(f, x0, y0, h, x1) Notice that we obtained a table of values. However, we can use the line command from Sage to plot the values .
As we pointed out, eulers_method is not very sophisticated. We have to use a very small step size to get good accuracy, and the method can generate errors if we are not careful. Fortunately, Sage has much better algorithms for solving initial value problems. One such algorithm is desolve_rk4, which implements the fourth order Runge-Kutta method.