Secant Method Python With Desired Accuracy

Introduction:

We are given a function and one interval a to b we have to find the solution of function f(x) for which f(x0) = 0.

Why is Secant Method: 

We know some equations like quadratic equations and cubic equations are solvable if a solution exists. but for some complex equations solution exists but it is complex to solve them using normal maths.

Secant Method:

Let, f(x) is given, and interval a to b is given. we want to find value of x = x0 where f(x0) = 0.

1) If f(a) * f(b) >= 0:
        Solution doesn't exists.

2) Find x using this x = a - f(a) * (b-a) / (f(b)-f(a))
        find f(x) for this x.

3) if f(a) * f(x) < 0:
        Update b = x

    Else if f(x) * f(b) < 0:
        Update a = x
    
    Else if f(x) == 0:
        x is solution.
    Else:
        The solution doesn't exist.

4) Repeat steps 2) and 3) until we get desired accuracy.

solving equations

f(x) = exp(x)+2^(-x) + 2*cos(x)-6 = 0

# f() function which return value of given function.
def f(x):
    # function given in question.
    return exp(x)+2**(-x) + 2*cos(x)-6

secent() function.


# secent() function which takes 4 arguments
# f = function, a and b interval, accuracy for accuracy of result
# return solution to given function.
def secant(f, a, b, accuracy=-1e-5):
    # if f(a)*f(b) >= 0 return None.
    if f(a)*f(b) >= 0:
        return None

    # set an = a.
    an = a
    # set bn = b.
    bn = b
    # index to count iterations.
    i = 0
    # create lest of x variables to store answers for each iterations.
    # which we use to find accuracy of 1e-5 or given.
    x = [-1e10, 1e10]
    # use while loop with this Condition
    # absolute differences of last two x value is greater or equal accuracy.
    # if not then while loop ends.
    while abs(x[-1]-x[-2]) >= accuracy:
        # find xn value.
        xn = an - f(an) * (bn-an) / (f(bn)-f(an))

        # find f value for xn.
        fxn = f(xn)
        # if fan*fxn<0 set bn = xn.
        if f(an)*fxn < 0:
            an = an
            bn = xn
        # elif fxn*fbn<0 set an = xn.
        elif fxn*f(bn) < 0:
            an = xn
            bn = bn
        # if fxn is 0 return xn.
        elif fxn == 0:
            return xn
        # else return None.
        else:
            return None
        # xn append to x.
        x.append(xn)
        # increment i.
        i += 1
    # return xn based on an and bn.
    return an - f(an) * (bn-an) / (f(bn)-f(an))


Printing result:


# print solution.
print("solving : exp(x)+2^(-x) + 2*cos(x)-6 = 0")
for i in range(16):
    # find accuracy
    accuracy = 10**(-i)
    # x value for fx = 0.
    x = secant(f, 1, 2, accuracy)
    # find fx.
    fx = f(x)

    print("x = {:<19}, with {:<6}, givens f(x) = {:<25}".format(x, accuracy, fx))

Please check this tool created by me to convert the number base from 2 to 20 here: number base converter

Output:

Secant Method Python With Desired Accuracy


Image:

Secant Method Python With Desired Accuracy


Comment if you have any questions...

Post a Comment

0 Comments