added ex02

This commit is contained in:
Daniel Knüttel 2019-02-19 14:14:20 +01:00
parent ff5eeb66b4
commit 21c3d3aa64

20
exam/ex02/main.py Normal file
View File

@ -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()