
Image by Gerd Altmann from Pixabay
Root Finding #1: Bisection Method
20 January 2024
Table Of Content
What are roots?
We will start this series by learning the easiest root finding algorightm: Bisection Method. We will explore the ins and outs of this algorithm, how it works and why it works.
To understand roots, it is essential to understand the definition of functions. Think of a function as a machine that follows precise rules - you put something in, and it gives you something back. For example, a simple function might take a number, double it, and subtract 4. But here's where roots come in: sometimes we're particularly interested in finding what we need to put into our function to get zero as the output. Why zero? Zero is special - it's like a balancing point where positive numbers meet negative numbers. When a function's output crosses zero, it switches from positive to negative (or vice versa), making zero a crucial threshold.
Approaximated upto 0.05
These "special inputs" that give us zero are what we call roots. Finding roots might sound abstract, but they show up in surprisingly practical situations. Imagine you're tracking two cars driving toward each other on a highway - finding when they meet means finding when the distance between them becomes zero. Or picture throwing a ball into the air - finding when it hits the ground means finding when its height becomes zero. Even in finance, if you're watching your bank account gradually decrease, finding when you'll run out of money means finding when your balance hits zero. In each case, we're really hunting for roots - those special input values that make something become zero.
Different types of roots require different way of handling. There are 3 types of roots:
- Simple Roots: has root at . They converge the fastest.
- Multiple Roots: , root at with multiplicity 2. They require modified algorithms.
- Complex Roots: , roots are . They exist in complex plane in form (real + imaginary component). They have conjugate pair property. They need specialized complex-number techniques.
Why Do We Need Numerical Methods?
Remember the root finding formula of ? It was,It was the first root finding formula we learned and still use to this day. It was so easy and straigtforward. However, certain equations, such as or , cannot be solved algebraically. These equations don't have closed-form solutions, meaning there is no straightforward formula to compute their roots or values. Numerical methods allow us to approximate these solutions by iteratively getting closer to the real root or value. This iterative process ensures that we can get as close as needed to the exact solution, even if it cannot be expressed explicitly. Exact math often gives you perfect theoretical answers (like or ) which lack true measurement ( and are irrational), but real-world usage require real value or approximations. Numerical methods help bridge that gap by giving us answers that are “close enough” to be practical.
Introduction to Numerical Methods for Root Finding
Two important thing to remember about numerical root finding is that,
- In numerical root finding, we run an iterative process that approximates the root. In each iteration/step, we do some calculation and get close to the original root.
- Since numerical root finding approximates the original root, there will be some error present in our approximation and in each iteration/step, we reduce this error and get a more accurate approximation.
Because we are dealing with approximations, it's crucial to understand the concept of convergence. A good root-finding method will converge towards the true root, meaning that with each iteration, the approximation gets progressively better. We continue this process until the change between successive approximations becomes sufficiently small, indicating that we are close enough to the actual root for our purposes.
Now, let's take a look at the Bisection Method, the simplest algorightm to find roots.
Bisection Method
Bisection Method is one of the easiest, reliable, begginer-friendly and convergence guarenteed method for finding roots of equations. It only works on continuous functions.
Core Idea (Optional)
A function has a root at if is within an interval and . This concept comes from the Intermediate Value Theorem.
Intermediate Value Theorem: If is a continuous function whose domain contains the interval , then it takes on any given value between and at some point within the interval.
It means that for any value between and , there's a value in for which . Bolzano's Theorem further specifies it in the context of roots.
Bolzano's Theorem: If a continuous function has values of opposite sign inside an interval, then it has a root in that interval.
In Bisection Method, we divide the interval in half in each step and see if the midpoint of that interval evaluates to zero in any of the divided section. If it does then that midpoint is our root. If it doesn't then we choose one of the divided interval (based on a condition discussed below) and again check if the midpoint of that interval evaluates to zero. We keep dividing the interval until the interval reaches our tolerance. Tolerance is the smallest interval we are willing to check. For example, if our tolerance is , then that means, when our interval is so small that the difference between the starting and ending point is or lower, we stop and select the midpoint of that interval as our root.
Steps (Mandatory)
- Initial Interval: Find two values, and , such that and have opposite signs. This ensures a root lies within the interval . So, we calculate and see if, .
- Midpoint: Calculate the midpoint c of the interval,
- Evaluate: Calculate the value of .
- Set New Interval:
- If , then is the root.
- If , then the root lies in the interval . Set .
- If , then the root lies in the interval . Set .
- Repeat: Repeat steps 2-4 until the interval becomes sufficiently small () or is close enough to zero.
Practical Example
Let's find a root of the function in the interval . We can see from the graph that there are multiple roots (-1.16877, 0 and 1.16877) and in our selected interval the actual root is .
Notice how the interval shrinks in each iteration and gets closer to the actual root. Zoom in on the graph to get a better view of the interval as it gets smaller.
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
, the new interval is
or , we stop the iteration and the last value of is our approximation of the actual root.
The approximated root is , which is very close to the actual root . The deviation is equal to the . By adjusting the , we can get a better approximation. However, in that case the iteration count will increase.
As we can see from the graphs, the interval is shrinking by half in each iteration and recalculating our actual root. Clearly, our approximation is getting closer to the actual root. In the last iteration, take a careful look at the graph and zoom in to see that the value we got from the last iteration is almost equal to the actual root . Take a quick look at the table below to get a bird's eye view of the entire process, it contains the essential data of each iteration.
Iterations | ||||
---|---|---|---|---|
Iteration 1 | 1.000000 | 2.000000 | 1.500000 | 3.468750 |
Iteration 2 | 1.000000 | 1.500000 | 1.250000 | 0.4736328 |
Iteration 3 | 1.000000 | 1.250000 | 1.125000 | -0.1842957 |
Iteration 4 | 1.125000 | 1.250000 | 1.187500 | 0.09308147 |
Iteration 5 | 1.125000 | 1.187500 | 1.156250 | -0.05732092 |
Iteration 6 | 1.156250 | 1.187500 | 1.171875 | 0.01480922 |
Iteration 7 | 1.156250 | 1.171875 | 1.164063 | -0.02200547 |
Iteration 8 | 1.164063 | 1.171875 | 1.167969 | -0.003787778 |
Iteration 9 | 1.167969 | 1.171875 | 1.169922 | 0.005463023 |
Iteration 10 | 1.167969 | 1.169922 | 1.168945 | 0.0008257340 |
Iteration 11 | 1.167969 | 1.168945 | 1.168457 | -0.001483990 |
Approximated Root: | 1.168457 |
Implementation
Below is the psuedocode for the implementation. Try to understand the steps and implement it on your own before looking at the solution.
1. Define the function f(x) that you want to find the root of. 2. Input: a - Start of the interval b - End of the interval tolerance - Acceptable error in the root value 3. Repeat until the width of the interval (b - a) is greater than tolerance: a. Calculate the midpoint: c = (a + b) / 2 b. Check if the function value at the midpoint is zero: If f(c) == 0: Return c (The exact root is found) c. Narrow down the interval: If f(a) * f(c) < 0: The root is in the left half, so set b = c Else: The root is in the right half, so set a = c 4. When the interval is small enough: Return (a + b) / 2 as the approximate root.
Here's the implementation of Bisection Method in a few commonly used languages:
function bisectionMethod(a, b, tolerance) {
while (b - a > tolerance) {
let c = (a + b) / 2;
if (f(c) === 0) {
return c;
}
if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
}
return (a + b) / 2;
}
const f = (x) => x**5 - x**3 - 1/2 * x
const root = bisectionMethod(1, 2, 0.001)
console.log("Root: ", root)
Important Terms
- Continuous Function: A continuous function is a smooth curve with no breaks, jumps, or sudden changes. It means you can draw the function without lifting your pencil from the paper.
- Convergence/Converging/Converge: This means an algorithm steadily approaches the true solution, systematically reducing the gap between the current estimate and the exact answer. It's like getting closer to a target with each step, where the distance between your current position and the target keeps shrinking.
- Multiplicity: Multiplicity refers to how many times a root appears or touches the x-axis. A root with multiplicity 2 means the function just touches the x-axis without crossing it. Higher multiplicity means the function "lingers" near the x-axis more.
- Complex Plane: The complex plane is a 2D graph where real numbers are plotted horizontally and imaginary numbers vertically. Each point represents a complex number as a coordinate, with representing the real part and the imaginary coefficient.
- Complex Conjugates: Complex conjugates are pairs of complex numbers that have the same real part but opposite imaginary parts. For example, 3 + 2i and 3 - 2i are complex conjugates: they're mirror images across the real axis.
Below is a non-continuous function , where Bisection Method won't work because for this function is undefined.
For example, has roots: with multiplicity 2 and with multiplicity 1.
At , function touches x-axis gently. At , function crosses x-axis normally.
Conclusion
Bisection Method is one of the easiest numerical methods. Understanding the core concepts and the implementation of this algorithm will help us get familiarized with the world of computational mathematics. The upcoming posts in this series will refer back to this post because this is one of the foudational algorithms in numerical analysis. So, if there's still any doubts or confusions, read the steps slowly and try to understand why each step was done, ask yourself, "If this step was not executed, how would it affect the end result?" and try to visualize using the graphs how each iteration depends on the previous iteration's data. This concludes the Bisection Method. In the next post, we will explore Newton-Raphson Method .
Part of series: Numerical Methods for Root Finding
Part 1 of 2