secant method Algorithm

The secant method is an iterative numerical technique used for finding the roots (or zeroes) of a real-valued function. It is a popular method because of its simplicity and ease of implementation, making it an attractive alternative to other root-finding algorithms, such as the bisection method and Newton-Raphson method. The secant method is based on the idea of approximating the function's derivative at a given point using the average slope of the function between two nearby points. This approximation helps to locate the next point that is closer to the root, which can then be used to generate a new approximation. To implement the secant method, we begin with two initial guesses, x0 and x1, for the root of the function. The algorithm then iteratively refines these values by computing the next point, x2, using the formula: x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)). The process is repeated, updating the values of x0 and x1 with the newly computed x2 and the previous x1, and calculating the next point using the same formula until the difference between the consecutive points is smaller than a pre-defined tolerance or a maximum number of iterations has been reached. The secant method converges faster than the bisection method but may not be as robust since it can sometimes fail to converge, depending on the choice of initial guesses and the properties of the function being analyzed.
# Implementing Secant method in Python
# Author: dimgrichr


from math import exp


def f(x):
    """
    >>> f(5)
    39.98652410600183
    """
    return 8 * x - 2 * exp(-x)


def SecantMethod(lower_bound, upper_bound, repeats):
    """
    >>> SecantMethod(1, 3, 2)
    0.2139409276214589
    """
    x0 = lower_bound
    x1 = upper_bound
    for i in range(0, repeats):
        x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
    return x1


print(f"The solution is: {SecantMethod(1, 3, 2)}")

LANGUAGE:

DARK MODE: