Search This Blog

Saturday, March 12, 2011

Newton-Raphson method


#Newton-Raphson method to find approximate root

from math import *
e=0.00001
#function definition
def f(x):
    return x*sin(x)+cos(x)

#differential of the function
def fp(x):
    return x*cos(x)


x0=float(input("Enter the approximate root \t"))

while 1:
    x1=x0-f(x0)/fp(x0)

    if abs(x0-x1)<e:
        break
    else:
        x0=x1

print "The best approximation to root is   %.5f"%x1

No comments:

Post a Comment