-1

I am learning python and scipy. In my self study, I am attempting to minimize following function using scipy.optimize

>   def opti_min(sr1_opt,sr3_opt):
>     op = np.sqrt(np.sum(sr1_opt)+np.sum(sr3_opt))
>     return op


opt = minimize(opti_min,strip,method = 'nelder-mead')

sr1_opt is a list:- [6.536055555554877e-07, 6.668262226847675e-07, 4.935691987513913e-07, 6.422222222223324e-07, 7.493249219562971e-07, 7.812500000001055e-07, 8.513020291362223e-07]

sr3_opt is also a list:- [4.127111433753003e-06, 4.419413067362789e-06, 4.721714700973233e-06]

strip is also a list to be used for initial values:- strip = [0.0025,0.005]

sr1_opt values are calculated as (actual value - strip[0])**2 and sr3_opt values are calculated as (actual value - strip[1])**2 for each entry in the list.

Hence, the required output is the strip values that will minimize the variable 'op'

the error I am getting is:- TypeError: opti_min() missing 1 required positional argument: 'sr3_opt'

What am I missing?

More importantly, which method should I use for constrained minimization if I want output for strip to be more than zero? Thanks in Advance

Deven
  • 1

1 Answers1

1

You can see a specific example from the docs for minimising a function of two parameters at the bottom of the page... https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html

In your case, you are treating opti_min as the function of a 2 dimensional variable strip:

values = [0.12, 0.65, ...] # whatever your values are

def opti_min(strip, values=values):
    # get your lists of edited values
    sr1_opt = [(value - strip[0])**2 for value in values]
    sr3_opt = [(value - strip[1])**2 for value in values]

    # perform your sum
    return np.sqrt(np.sum(sr1_opt)+np.sum(sr3_opt))
    
# init your initial estimate
strip0 = np.array([0.0025,0.005])

res = minimize(opti_min, strip0, method='nelder-mead')

# print result
print(res.x)
Matt
  • 1,196
  • 1
  • 9
  • 22