Fixed Point Iteration Python Program (with Output)

Python program to find real root of non-linear equation using Fixed Point Iteration Method. This method is also known as Iterative Method.

Fixed Point Iteration Method Python Program


# Fixed Point Iteration Method
# Importing math to use sqrt function
import math

def f(x):
    return x*x*x + x*x -1

# Re-writing f(x)=0 to x = g(x)
def g(x):
    return 1/math.sqrt(1+x)

# Implementing Fixed Point Iteration Method
def fixedPointIteration(x0, e, N):
    print('\n\n*** FIXED POINT ITERATION ***')
    step = 1
    flag = 1
    condition = True
    while condition:
        x1 = g(x0)
        print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
        x0 = x1

        step = step + 1
        
        if step > N:
            flag=0
            break
        
        condition = abs(f(x1)) > e

    if flag==1:
        print('\nRequired root is: %0.8f' % x1)
    else:
        print('\nNot Convergent.')

# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')

# Converting x0 and e to float
x0 = float(x0)
e = float(e)

# Converting N to integer
N = int(N)


#Note: You can combine above three section like this
# x0 = float(input('Enter Guess: '))
# e = float(input('Tolerable Error: '))
# N = int(input('Maximum Step: '))

# Starting Newton Raphson Method
fixedPointIteration(x0,e,N)

Output

Enter Guess: 2
Tolerable Error: 0.00001
Maximum Step: 10


*** FIXED POINT ITERATION ***
Iteration-1, x1 = 0.577350 and f(x1) = -0.474217
Iteration-2, x1 = 0.796225 and f(x1) = 0.138761
Iteration-3, x1 = 0.746139 and f(x1) = -0.027884
Iteration-4, x1 = 0.756764 and f(x1) = 0.006085
Iteration-5, x1 = 0.754472 and f(x1) = -0.001305
Iteration-6, x1 = 0.754965 and f(x1) = 0.000281
Iteration-7, x1 = 0.754859 and f(x1) = -0.000060
Iteration-8, x1 = 0.754882 and f(x1) = 0.000013
Iteration-9, x1 = 0.754877 and f(x1) = -0.000003

Required root is: 0.75487680