Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
0 views

Python Codes for root finding

Uploaded by

m.mohsinraza1985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Codes for root finding

Uploaded by

m.mohsinraza1985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

from sympy import*

import math

print("Enter the type of method you want to choose to calculate the


roots: ")
print("1) Newton Raphson Method\n2) Secant Method\n3) Fixed Point
Method")

val=int(input())

#Newton Method
def f(x):
return x ** 3 - 5 * x - 9

def g(x):
return Derivative(3 * x ** 2 - 5)

def newtonRaphson(x0, e, N):


step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break

x1 = x0 - f(x0) / 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.')

#secant method
def secant(x0, x1, e, N):
step = 1
condition = True
while condition:
if f(x0) == f(x1):
print('Divide by zero error!')
break

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)))
x0 = x1
x1 = x2
step = step + 1

if step > N:
print('Not Convergent!')
break

condition = abs(f(x2)) > e


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

#fixed point method


def t(x):
return 1 / math.sqrt(1 + x)

def fixedPointIteration(x0, e, N):


step = 1
flag = 1
condition = True
while condition:
x1 = t(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.')

#bisection method
def bisection(x0, x1, e):
step = 1
condition = True
while condition:
x2 = (x0 + x1) / 2
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)

#false position method


def falsePosition(x0,x1,e):
step = 1
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)

#operating according to the required method


if val==1:
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')

x0 = float(x0)
e = float(e)

N = int(N)

newtonRaphson(x0, e, N)
elif val==2:
x0 = input('Enter First Guess: ')
x1 = input('Enter Second Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')

x0 = float(x0)
x1 = float(x1)
e = float(e)
N = int(N)
secant(x0, x1, e, N)
elif val==3:

x0 = input('Enter Guess: ')


e = input('Tolerable Error: ')
N = input('Maximum Step: ')

x0 = float(x0)
e = float(e)

N = int(N)

fixedPointIteration(x0, e, N)
elif val==4:
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

x0 = float(x0)
x1 = float(x1)
e = float(e)

if f(x0) * f(x1) > 0.0:


print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')
else:
bisection(x0, x1, e)
elif val==5:

x0 = input('First Guess: ')


x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

x0 = float(x0)
x1 = float(x1)
e = float(e)

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)

else:
print("Wrong Input! Please select a valid option.")

You might also like