Python/arithmetic_analysis/newton_method.py

19 lines
447 B
Python
Raw Normal View History

# Newton's Method - https://en.wikipedia.org/wiki/Newton%27s_method
2018-10-19 12:48:28 +00:00
def newton(function,function1,startingInt): #function is the f(x) and function1 is the f'(x)
x_n=startingInt
while True:
x_n1=x_n-function(x_n)/function1(x_n)
if abs(x_n-x_n1) < 10**-5:
2018-10-19 12:48:28 +00:00
return x_n1
x_n=x_n1
def f(x):
return (x**3) - (2 * x) -5
2018-10-19 12:48:28 +00:00
def f1(x):
return 3 * (x**2) -2
2018-10-19 12:48:28 +00:00
if __name__ == "__main__":
print(newton(f,f1,3))