Root Finding Methods

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Newton Raphson Method

from numpy import* while abs(f(x1))>0.0001:


x0 = float(input('Enter x0=x1
Guess: ')) x1 = x0 - f(x0)/g(x0)
def f(x): print("The root is ",x1)
return cos(x)-x*exp(x)
#(0,1)
def g(x):
return -sin(x)-(x+1)*exp(x)
x1 = x0 - f(x0)/g(x0)
Bisection Method
from numpy import* if f(x0)*f(x1) <0:
x0 = float(input("enter the while abs(f(x2))>0.0001:
first limit ")) if f(x0)*f(x2)>0:
x1 = float(input("enter the x0=x2
first limit "))
else:
x2 = (x0+x1)/2
x1 = x2

def f(x): x2=(x0+x1)/2


return cos(x)-x*exp(x) print("The root is ",x2)
#(0,1) else:
print("the limits are not
valid")
Regula False Method
from numpy import* if f(x0)*f(x1) <0:
x0 = float(input("enter the while abs(f(x2))>0.0001:
first limit ")) if f(x0)*f(x2)>0:
x1 = float(input("enter the x0=x2
first limit ")) else:
x2 = x2 = (x0 *f(x1)- x1 = x2
x1*f(x0))/(f(x1)-f(x0)) x2= (x0 *f(x1)-
x1*f(x0))/(f(x1)-f(x0))
def f(x): print("The root is ",x2)
return cos(x)-x*exp(x) #(0,1) else:
print("the limits are not valid")
Secant Method
from numpy import* while abs(f(x2))>0.0001:
x0 = float(input("enter the first if f(x0)*f(x2)>0:
limit ")) x0=x2
x1 = float(input("enter the first else:
limit ")) x1 = x2
x2 = x2 = (x0 *f(x1)-
x2= (x0 *f(x1)-
x1*f(x0))/(f(x1)-f(x0))
x1*f(x0))/(f(x1)-f(x0))
print("The root is ",x2)
def f(x):
return cos(x)-x*exp(x) #(0,1)

You might also like