scientific-programming-exer.../ex_47.py

20 lines
354 B
Python
Raw Normal View History

2019-01-23 14:27:28 +00:00
import numpy as np
from scipy.optimize import minimize
2019-01-23 15:47:12 +00:00
import matplotlib.pyplot as plt
2019-01-23 14:27:28 +00:00
f = lambda x: x*np.sin(7*x)*np.exp(-(x - 2)**2)
x0 = f(np.arange(-20, 20, 0.1)).min()
m = minimize(f, x0, method="CG")
print(m)
2019-01-23 15:47:12 +00:00
g = lambda x0: minimize(f, x0, method="CG").x
x0s = np.arange(-3, 3, 0.001)
xs = [g(x0) for x0 in x0s]
plt.plot(x0s, xs)
plt.show()