1

I have a function with two input variables we are looking to optimise. The function returns an output and we want to minimise this output. What is the best way to do this in Python?

Presently the functions input variables have been hardcoded, the goal would be to iterate over a range and find the optimal for both parameters.

I've looked into scipy but unsure how to utilise it in my situation.

The output of my code can be seen below.

output of my code

def average_receptance(K_t, C_t):

    frequency_matrix = np.array([])
    alphabetaE11_matrix = np.array([])

    for i in range(first_natural_frequency - natural_frequency_delta,first_natural_frequency + natural_frequency_delta):
    
        frequency_matrix = np.append(frequency_matrix, i)
        alphabetaE11_matrix = np.append(alphabetaE11_matrix, math.log(abs(receptance(K_t, C_t, i))))
        
    receptance_average = np.average(alphabetaE11_matrix)

    return receptance_average

print(average_receptance(600000, 50))
print(average_receptance(626759, 50))
print(average_receptance(650000, 50))

print(average_receptance(600000, 100))
print(average_receptance(626759, 100))
print(average_receptance(650000, 100))

print(average_receptance(600000, 150))
print(average_receptance(626759, 150))
print(average_receptance(650000, 150))
pppery
  • 3,731
  • 22
  • 33
  • 46
  • What did you try? There are algos to fit a curve through points you got from somewhere - you can get its maxima / minima with 1st derivative = 0 or use something like simulated annealing / hill climbing – Patrick Artner Jan 25 '22 at 11:43
  • [python-numpy-scipy-curve-fitting](https://stackoverflow.com/questions/19165259/python-numpy-scipy-curve-fitting) && [how-to-find-local-minima-using-scipy](https://stackoverflow.com/questions/66851413/how-to-find-local-minima-using-scipy) – Patrick Artner Jan 25 '22 at 11:43
  • Have you tried [`scipy.optimize.minimize`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html)? – a_guest Jan 25 '22 at 12:10

1 Answers1

0

You can use scipy.optimize.minimize to minimize a scalar function with one or more variables. For this, first change your function as:

def average_receptance(x):

    K_t = x[0]
    C_t = x[1]

    frequency_matrix = np.array([])
    alphabetaE11_matrix = np.array([])

    for i in range(first_natural_frequency - natural_frequency_delta,first_natural_frequency + natural_frequency_delta):
    
        frequency_matrix = np.append(frequency_matrix, i)
        alphabetaE11_matrix = np.append(alphabetaE11_matrix, math.log(abs(receptance(K_t, C_t, i))))
        
    receptance_average = np.average(alphabetaE11_matrix)

    return receptance_average

then use minimize function of scipy, to minimize the variables. You need to pass an initial guess though for the optimization to start. You can do this as follows:

x0 = [600000, 50] # -> example guess for K_t and C_t
res = minimize(average_receptance, x0, method="Nelder-Mead",
               options={'disp':True, 'fatol':1e-04})
print(res)

The above code will minimize both the parameters of your function.

Muhammad Mohsin Khan
  • 1,444
  • 7
  • 16
  • 23
  • Thank you so much!! It works! This is the scypi code I had which wasn't working: i.imgur.com/3Uflt9G.png Thanks again ! :) – Admirable-Sun-1263 Jan 26 '22 at 04:07
  • Glad that it worked for you! If my answer solved your problem, you should have a look at [what should I do when someone answers my question](https://stackoverflow.com/help/someone-answers) :). – Muhammad Mohsin Khan Jan 26 '22 at 08:06