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