Reading: Heath §5.5.1, 5.5.3, 5.5.4, 5.5.7
Notes: Again, there will be no formal practical this week.
This algorithm is based on the simple idea that if a continuous function
has opposite signs at two points
and
, then it must have a root on the
interval
.
Take the midpoint
of the interval
.
Depending on the sign of
we can determine in which of the subintervals
and
the root lies.
Repeating this process we get the bisection algorithm.
The bisection algorithm is easy to implement in Scilab. The following
function computes the midpoint of the current interval [a,b],
tests the sign
of f(m), and adjusts the endpoints until the length of the
interval is less than tol.
function [a,b] = bisect (f, a0, b0, tol)
if (sign(f(a0)) == sign(f(b0)))
error ("function has same sign at both endpoints")
end
a = a0
b = b0;
while (abs(a-b) > tol)
m = a + (b-a)/2
if (sign(f(m)) == sign(f(a)))
a = m
else
b = m
end
end
endfunction
We will use the example of Kepler's equation from the previous lecture
-->function y = kepler (e)
--> y = e - 1 - 0.5*sin(e)
-->endfunction
-->[a,b] = bisect(kepler, 1, 2, 1e-5)
b =
1.498703
a =
1.4986954
-->[a,b] = bisect(kepler, 1, 2, 1e-10)
b =
1.4987011
a =
1.4987011
It is easy to see that the bisection algorithm is linearly convergent with
rate of convergence
, since at each iteration the size of the
interval containing the root is decreased by a factor of 2. Further, it is easy to calculate
the number of iterations needed to achieve a given accuracy. Starting with
an interval
, after
iterations the length of the
interval containing the
root is
, so to obtain an interval of length
requires
One important property of the bisection algorithm, not shared by most other algorithms, is that it always converges provided the function is continuous and we start from interval on which the function has opposite signs at the two endpoints.
The idea behind Newton's method is equally simple. Given an approximation
to a solution of
, approximate
by its tangent
at
and take
the point at which the tangent intersects the
-axis as the
next approximation
A simple geometric calculation then gives
Newton's method is easy to program in Scilab. The following function
performs n iterations of Newton's method starting with the initial
approximation x0. Note that we need the derivative of the function
as well as the function itself.
function x = newton (x0, f, df, n)
x = x0
for k = 1:n
x = x - f(x)/df(x)
end
endfunction
Applying this to Kepler's equation gives
-->function dy = dkepler (e)
--> dy = 1 - 0.5*cos(e)
-->endfunction
-->x = newton(1.5, kepler, dkepler, 5)
x =
1.4987011
Newton's method can be viewed as a fixed point iteration
To examine the convergence of Newton's method, we can modify our
function newton so that it returns the sequence of iterations:
function x = newtons (x0, f, df, n)
x = zeros(1,n+1)
x(1) = x0
for k = 1:n
x(k+1) = x(k) - f(x(k))/df(x(k))
end
endfunction
For our example:
-->x = newtons(1.5, kepler, dkepler, 5)
x =
column 1 to 5
! 1.5 1.4987016 1.4987011 1.4987011 1.4987011 !
column 6
! 1.4987011 !
Comparing the iterations with the final value
-->x-x(6) ans = ! 0.0012989 4.361E-07 4.907E-14 0. 0. 0. !shows that it has converged to full accuracy in 3 iterations! The quadratic convergence, that is the error at each iteration is proportional to the square of the error at the previous iteration, is also apparent.
Beware that Newton's method, like other fixed point methods, does not necessarily converge to a solution. To ensure convergence we need to start with an initial approximation sufficiently close to the solution.
The secant method is similar to Newton's method except that
we approximate the function by a secant rather than by the tangent.
Given two approximations
and
to a solution of
, approximate
by its
secant at
and
and take the point at which the
secant intersects the
-axis as the next approximation
.
Again a simple geometric calculation gives
Below is a Scilab function for the secant method. It stores the iterations
and takes two initial approximations x0 and x1 to get started.
function x = secant (x0, x1, f, n)
x = zeros(1, n+2)
x(1) = x0
x(2) = x1
for k = 2:n+1
x(k+1) = x(k) - f(x(k))*(x(k) - x(k-1))/(f(x(k)) - f(x(k-1)))
end
endfunction
For Kepler's equation it gives
-->x = secant(2.0, 1.5, kepler, 5)
x =
column 1 to 6
! 2. 1.5 1.498849 1.4987012 1.4987011 1.4987011 !
column 7
! 1.4987011 !
We have to a bit careful using the function secant
since it will result in a division by
zero when two successive iterates are the same, a situation which will occur
whenever it has converged to full accuracy.
The secant algorithm has order of convergence
(see Heath, p223)
except at multiple roots when, like Newton's method, it is only linearly
convergent. Again, like Newton's method, it requires good
initial approximations to ensure convergence.
The essential properties of the algorithms we have studied can be summarized as follows
| Method | Always | Order of | Requires |
| Converges | Convergence | Derivatives | |
| Bisection | Yes | Linear | No |
| Secant | No | 1.62 | No |
| Newton | No | Quadratic | Yes |
It is clear that each of these algorithms has its advantages
and disadvantages.
The methods used in practice, for example Scilab's fsolve,
use a combination of methods to obtain the advantages of each.
These algorithms tend to be quite sophisticated and rely
on years of accumulated experience.
A simple way to combine the bisection method with other more rapidly convergent algorithms is to start with an interval known to contain the solution and apply the faster method as long as it produces results in the interval. When that fails, bisection can be applied to reduce the width of the interval and then the faster method tried again.
Scilab's fsolve is easy to use, it simply requires an initial
approximation
-->x = fsolve(1.5, kepler)
x =
1.4987011
This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -split +0 lecture.tex
The translation was initiated by amth247 on 2003-05-13