The problem
Root finding solves f(x) = 0: given a real-valued function, find the values of x where it crosses zero. Only a few equations can be solved with closed formulas, so most are handled by iterative methods that produce successively better approximations.
Bisection method
Bisection needs two starting points, a and b, where f changes sign — the root is guaranteed to lie between them. Each step evaluates the midpoint and keeps the half-interval where the sign still changes:
do while (abs(b - a) > tol)
x = (a + b) / 2.0
if (f(a) * f(x) < 0.0) then
b = x
else
a = x
end if
end doThe interval halves every step, so the method is dependable — if a sign change exists, bisection finds the root — but it converges linearly: each step buys roughly one more bit of accuracy, and high precision takes many steps.
Newton-Raphson method
The Newton-Raphson method uses the derivative. Starting from a guess x, each step follows the tangent line to where it crosses the axis:
xnew = x − f(x) / f′(x)
Near a simple root it converges quadratically — the number of correct digits roughly doubles each step — which makes it much faster than bisection when it works. The costs: the derivative must be available, and a poor starting guess can send the iteration the wrong way or fail to converge at all. A common pattern pairs the two methods: bisection to get close, Newton to finish quickly.
Choosing a method
- Need certainty and have a bracketing interval? Use bisection.
- Have a good guess and an easy derivative? Newton-Raphson is the fast choice.
- No derivative available? A secant-style approach replaces the derivative with a difference of previous values, at some cost in reliability.
Whichever method you choose, always guard the loop with a maximum iteration count as well as a tolerance.