-
Notifications
You must be signed in to change notification settings - Fork 1
/
newton.py
35 lines (27 loc) · 810 Bytes
/
newton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import derivative
def newton(f):
x = 2
x_a = x
while True:
x = x_a - derivative(f, x_a, n=1, dx=1e-7) / derivative(f, x_a, n=2, dx=1e-7)
if abs(x - x_a) < 1.0:
break
return x
def main():
x = np.linspace(-5, 7, 30)
y = x**3 - 2 * x**2 + x + 3
f = lambda x: x**3 - 2 * x**2 + x + 3
ans_x = round(newton(f), 3)
ans_y = round(f(ans_x), 3)
print("Local min: ({0},{1})".format(ans_x, ans_y))
plt.plot(x, y)
plt.title("y = x ** 3 - 2 * x ** 2 + x + 3")
label = "Local min: ({0},{1})".format(ans_x, ans_y)
plt.annotate(
label, xy=(ans_x, ans_y), arrowprops=dict(facecolor="red", shrink=0.05)
)
plt.show()
if __name__ == "__main__":
main()