Python Programming
Basics
Numerical Methods
- Bisection Method
- Secant Method
- Regular Falsi (False Position) Method
- Newton Raphson Method
- Gauss Elimination Method
- Gauss Jordan Method
- Gauss-Seidel Method
- Lagrange Interpolation Method
- Newton Divided Difference Interpolation
- Newton Forward Difference Interpolation
- Newton Backward Difference Interpolation
- Trapezoidal Rule
- Simpson 1/3rd Rule
- Simpson 3/8 Rule
- Euler’s Method
- Euler’s Modified Method
- Runge-Kutta 2nd Order Method
- Runge-Kutta 4th Order Method
- Cubic Spline Method
- Bilinear Interpolation Method
- Milne’s Method
- More topics coming soon…
Advance Numerical Methods
The method of false position (also known as the “regular falsi” method) is a root-finding algorithm.
It uses a succession of roots of secant lines to approximate a root of a function.
It can be used to find a solution to an equation of the form f(x) = 0, where f is a continuous function defined on an interval [a, b].
Regular Falsi (False Position) method in Python:
- Function
f(x) = $x^{2} – 3$
def false_position(f, a, b, tol=1e-9, maxiter=100):
"""
f : Function for which we are trying to find a root.
a, b : Interval in which the root is sought.
tol : Tolerance for the root. The default value is 1e-9.
maxiter : Maximum number of iterations to perform.
"""
#Regular Falsi Method [By Bottom Science]
for i in range(maxiter):
# Compute the value of the function at the midpoint of the interval
c = (a * f(b) - b * f(a)) / (f(b) - f(a))
fc = f(c)
if fc == 0 or (b - a) / 2 < tol:
# If the function at the midpoint is zero or the interval is small enough,
# then we have found the root
return c
# Compute the function at the endpoints of the interval
fa = f(a)
fb = f(b)
if (fa > 0 and fc > 0) or (fa < 0 and fc < 0):
# If the signs of the function at the endpoints and at the midpoint are the same,
# then we can narrow down the interval to [c, b]
a = c
else:
# Otherwise, we can narrow down the interval to [a, c]
b = c
# If we reach this point, then the maximum number of iterations has been exceeded
raise Exception("Maximum number of iterations exceeded")
root = false_position(lambda x: x**2 - 3, 1, 2)
print(root)