In [5]:
import numpy as np
import scipy.optimize as opt
from matplotlib import pyplot as plt
import math
In [6]:
def f(x):
return math.sin(x / 5) * math.exp(x / 10) + 5 * math.exp(-x / 2)
In [9]:
for x in range(1, 31):
print(x, opt.minimize(f, x).x)
In [11]:
opt.minimize(f, 2, method='L-BFGS-B')
Out[11]:
In [12]:
opt.minimize(f, 30, method='L-BFGS-B')
Out[12]:
In [13]:
%matplotlib inline
x = [x for x in range(1, 31)]
y = [f(x) for x in range(1, 31)]
plt.plot(x, y)
Out[13]:
In [16]:
result = opt.differential_evolution(f, [(1, 30)])
print(result.x, result.fun)
In [19]:
def h(x):
return int(f(x))
x = [x for x in range(1, 31)]
y = [h(x) for x in range(1, 31)]
plt.plot(x, y)
Out[19]:
In [20]:
opt.minimize(h, 30, method='L-BFGS-B')
Out[20]:
In [21]:
result = opt.differential_evolution(h, [(1, 30)])
print(result.x, result.fun)
In [ ]: