= Solution
The first instruction asks for maximization, whereas the later parts refer to minimization. For the literal maximization task, minimize the negative objective:
``
import numpy as np
from scipy import optimize as opt
def f(x):
return 1 + np.exp(-x*x/5) * np.sin(5*x)
maximum = opt.minimize(lambda z: -f(z[0]), x0=[-1.0])
print(maximum.x[0], f(maximum.x[0]))
``
This gives a <local maximum> near \b[$x=-0.927662$, $f(x)=1.839576$]. If minimization was intended, use `opt.minimize(lambda z: f(z[0]), x0=[-1.0])`, which instead returns a <local minimum> near \b[$x=-1.546182$, $f(x)=0.384751$]. The optimizer's stopping criterion establishes local numerical convergence, not a certificate of a global optimum.
Back to article page