False Position Method Python Program (with Output)

In this tutorial, we have implemented False Position method (also known as Regula Falsi method) to find real root of non-linear equation using python programming language.


# Defining Function
def f(x):
    return x**3-5*x-9

# Implementing False Position Method
def falsePosition(x0,x1,e):
    step = 1
    print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***')
    condition = True
    while condition:
        x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
        print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

        if f(x0) * f(x2) < 0:
            x1 = x2
        else:
            x0 = x2

        step = step + 1
        condition = abs(f(x2)) > e

    print('\nRequired root is: %0.8f' % x2)


# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

# Converting input to float
x0 = float(x0)
x1 = float(x1)
e = float(e)

#Note: You can combine above two section like this
# x0 = float(input('First Guess: '))
# x1 = float(input('Second Guess: '))
# e = float(input('Tolerable Error: '))


# Checking Correctness of initial guess values and false positioning
if f(x0) * f(x1) > 0.0:
    print('Given guess values do not bracket the root.')
    print('Try Again with different guess values.')
else:
    falsePosition(x0,x1,e)

Output

First Guess: 2
Second Guess: 3
Tolerable Error: 0.00001


*** FALSE POSITION METHOD IMPLEMENTATION ***
Iteration-1, x2 = 2.785714 and f(x2) = -1.310860
Iteration-2, x2 = 2.850875 and f(x2) = -0.083923
Iteration-3, x2 = 2.854933 and f(x2) = -0.005125
Iteration-4, x2 = 2.855180 and f(x2) = -0.000312
Iteration-5, x2 = 2.855196 and f(x2) = -0.000019
Iteration-6, x2 = 2.855196 and f(x2) = -0.000001

Required root is: 2.85519648