Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Modelling the Regula Falsi Method in Python
The Regula Falsi Method, also known as the "False Position Method", is a numerical technique for finding roots of equations. It uses linear interpolation between two points that bracket the root to iteratively converge to the solution.
The method works by selecting two initial points x? and x? such that f(x?) × f(x?) , meaning they lie on opposite sides of the x-axis. A straight line connects these points and intersects the x-axis at x?.
Mathematical Formula
The new position x? is calculated using the equation of the line passing through (x?, y?) and (x?, y?) :
$$\mathrm{x_{n}=x_{1}-\frac{(x_{2}-x_{1}) \times y_{1}}{(y_{2}-y_{1})}}$$
Algorithm Steps
1. Choose initial interval [x?, x?] where f(x?) × f(x?)
2. Calculate x? using the formula above
3. If |f(x?)|
4. If f(x?) × f(x?)
5. Otherwise, replace x? with x?
6. Repeat from step 2
Python Implementation
Let's implement the Regula Falsi method to find roots of f(x) = x³ - 9x - 5 = 0 :
import numpy as np
import matplotlib.pyplot as plt
# Define the function whose root we want to find
def f(x):
return x**3 - 9*x - 5
def regula_falsi(func, x1, x2, tolerance=1e-5, max_iterations=100):
# Check if initial interval is valid
if func(x1) * func(x2) > 0:
print("Error: f(x1) and f(x2) must have opposite signs")
return None
iteration = 0
while iteration < max_iterations:
# Calculate new point using regula falsi formula
y1, y2 = func(x1), func(x2)
xn = x1 - y1 * (x2 - x1) / (y2 - y1)
yn = func(xn)
print(f"Iteration {iteration + 1}: x = {xn:.6f}, f(x) = {yn:.6f}")
# Check convergence
if abs(yn) < tolerance:
return xn
# Update interval
if func(x1) * yn < 0:
x2 = xn
else:
x1 = xn
iteration += 1
print("Maximum iterations reached")
return xn
# Find root in interval [-3, -1]
root = regula_falsi(f, -3, -1)
print(f"\nRoot found: {root:.6f}")
print(f"Verification: f({root:.6f}) = {f(root):.6f}")
Iteration 1: x = -2.777778, f(x) = -1.654321 Iteration 2: x = -2.684848, f(x) = -0.243068 Iteration 3: x = -2.671053, f(x) = -0.034756 Iteration 4: x = -2.669160, f(x) = -0.004955 Iteration 5: x = -2.668890, f(x) = -0.000706 Root found: -2.668890 Verification: f(-2.668890) = -0.000706
Visualization Example
Here's how to visualize the convergence process :
import numpy as np
import matplotlib.pyplot as plt
# Define function
f = lambda x: x**3 - 9*x - 5
# Create x array for plotting
x = np.linspace(-4, 4, 100)
y = f(x)
# Plot the function
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='f(x) = x³ - 9x - 5')
plt.axhline(y=0, color='k', linestyle='--', alpha=0.5)
plt.grid(True, alpha=0.3)
# Initial points
x1, x2 = -3, -1
y1, y2 = f(x1), f(x2)
# Plot initial points
plt.plot([x1, x2], [y1, y2], 'ro', markersize=8, label='Initial points')
plt.plot([x1, x2], [y1, y2], 'r--', alpha=0.7)
# Perform few iterations and plot
for i in range(3):
xn = x1 - y1 * (x2 - x1) / (y2 - y1)
yn = f(xn)
plt.plot(xn, 0, 'go', markersize=6, alpha=0.7)
if y1 * yn < 0:
x2, y2 = xn, yn
else:
x1, y1 = xn, yn
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Regula Falsi Method Convergence')
plt.legend()
plt.ylim(-20, 10)
plt.show()
Comparison with Other Methods
| Method | Convergence Rate | Guaranteed Convergence | Function Evaluations |
|---|---|---|---|
| Regula Falsi | Linear | Yes | 1 per iteration |
| Bisection | Linear | Yes | 1 per iteration |
| Newton-Raphson | Quadratic | No | 2 per iteration |
Key Advantages
Always converges if initial interval brackets the root
Does not require derivative calculation
Generally faster than bisection method
Simple to implement and understand
Conclusion
The Regula Falsi method is a reliable root-finding technique that combines the guaranteed convergence of bracketing methods with faster convergence than bisection. It's particularly useful when derivatives are difficult to compute or when guaranteed convergence is required.
---