Use reproducible random starts and retain the best successful result:
rng = np.random.default_rng(2024)
starts = rng.uniform(-4, 4, size=100)
results = [opt.minimize(lambda z: f(z[0]), x0=[x0]) for x0 in starts]
successful = [result for result in results if result.success]
best = min(successful, key=lambda result: result.fun)
print(best.x[0], best.fun)
The estimate is , . This random-restart optimization improves coverage of the different attraction regions, but their number alone does not guarantee global optimality.
Here an analytic check is possible. For , the envelope gives , above the candidate. On , . In the remaining interval , the derivative condition is equivalent, with , to . The left side decreases strictly from to while the right side increases, giving one minimum. This certifies the selected well as global rather than relying only on the restart experiment.