pythontrigonometrynumerical-methodsnewtons-methodnumerical-analysis

Result error in implementation of trigonometric function by Newton Raphson method in PYTHON


trigonometric function by Newton method is giving wrong results

def func(x):
    return radians(math.sin(x)+log(x)+1)

def derivFunc(x):
    return radians((1/x) + math.cos(x))

#sin(x)+log(x)+1 --is the function i want to apply method on

**# Function to find the root**
def newtonRaphson(x):
    h = func(x) / derivFunc(x)
    while abs(h) >= 0.0001:
        h = func(x) / derivFunc(x)
        # x(i+1) = x(i) - f(x) / f'(x)
        x = x - h
    print("The value of the root is : ",
          "%.4f" % x)

x0 = 0.03  
newtonRaphson(x0)

Solution

  • The problem is the radians, which multiplies a part of an expression by pi/180.0. Remove all of its mentions, and it should be fine.

    EDIT

    The below works absolutely fine now:

    import math
    def func(x):
        return math.sin(x)+math.log10(x)+1
    
    def derivFunc(x):
        return (1/x) + math.cos(x)
    #sin(x)+log(x)+1 --is the function i want to apply method on
    
    # Function to find the root
    def newtonRaphson(x):
        h = func(x) / derivFunc(x)
        while abs(h) >= 0.0001:
            h = func(x) / derivFunc(x)
            # x(i+1) = x(i) - f(x) / f'(x)
            x = x - h
        print("The value of the root is : ",
              "%.4f" % x)
    
     x0 = 0.03  
     newtonRaphson(x0)