scientific-programming-exer.../exam/ex02/main.py

21 lines
546 B
Python
Raw Permalink Normal View History

2019-02-19 13:14:20 +00:00
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()