Math & Physics Problems Wikia
Register
m (Stevenzheng moved page Newton's Method to Newton's Method with Python)
(Adding categories)
Line 27: Line 27:
 
[[Category:Scientific computing]]
 
[[Category:Scientific computing]]
 
[[Category:Differentiation]]
 
[[Category:Differentiation]]
  +
[[Category:Python]]

Revision as of 00:33, 4 November 2019

Newton's method

Derivative function

def derivative(f,x):
    h = 0.00000001 
    return (f(x+h) - f(x))/h

Newton's method function

def newton_method(f, x):

    tolerance = 0.00000001 
    
    while True: 
        x1 = x - f(x)/derivative(f,x) 
        t = abs(x1 - x)
 
        if t < tolerance: 
        break         
        x = x1 
    return x

Test and results

def f1(x): 
    return x**3-1 

root = newton_method(f1, 1) 
print(root) #RESULT: 1