rul30
rul30

Reputation: 473

Why does scipy.optimize.minimize not find the minimum?

I am looking to find for which (x) the following function is minimal given a parameter dim.
This is the function:

def func(x, dim):
    return np.abs(np.abs(np.mean(np.sqrt(np.sum(np.diff(
        np.random.rand(100000,dim,2)/x, axis=2)**2, axis=1))))
        - 1/3)

And this is how it looks:

for xx in np.arange(1,5,0.1):
    plt.scatter(xx, func(xx,2), color='blue')

enter image description here

But when I try to find the x value which should be around 1.5 the result very close to my x0 guess (here around 1.0).

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize


params = minimize(func, x0=1, args=(2))

I also tried different solvers, but I cannot get it to minimize.

Upvotes: 0

Views: 268

Answers (1)

Ingo
Ingo

Reputation: 1263

Your function needs to be deterministic for the minimization to work. So you need to remove your call to np.random.rand. Once solution could be to generate those random numbers once at the beginning and fix them throughout the minimization.

Upvotes: 1

Related Questions