From 21c3d3aa6433c99abd6116571bb3639a29fbca28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Tue, 19 Feb 2019 14:14:20 +0100 Subject: [PATCH] added ex02 --- exam/ex02/main.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 exam/ex02/main.py diff --git a/exam/ex02/main.py b/exam/ex02/main.py new file mode 100644 index 0000000..1e13e6b --- /dev/null +++ b/exam/ex02/main.py @@ -0,0 +1,20 @@ +from scipy.optimize import basinhopping +import numpy as np +import matplotlib.pyplot as plt + +f = lambda x: np.exp(-x**2) * np.sin(12 * x) / (5 * x) + +x0 = 0.2 + +minimum = basinhopping(f, x0).x +maximum = basinhopping(lambda x: -f(x), x0).x + +X = np.arange(-10, 10, 0.01) + +p1, = plt.plot(X, f(X), "r-", label="f(x)") +p2, = plt.plot([minimum], [f(minimum)], "bo", label="Minimum") +p2, = plt.plot([-minimum], [f(minimum)], "bo", label="Minimum") +p3, = plt.plot([maximum], [f(maximum)], "go", label="Maximum") + +plt.legend(handles=[p1, p2, p3]) +plt.show()