Search code examples
pythonscipy-optimize

Add argument in scipy.optimize


I am trying to use scipy.optimize.minimize (simplex method) for minimizing the following function

argmin (sum(i=0 to 9) (a*i - b_i)**2)

I would like to obtain the value of a. I have 10 values of b_i (i have it already calculated outside the function) that i want to insert in the function

def f(a, b):
    func = 0
    for i in range(10):
        func+= (a*i - b[i])**2
    return func
init = [1]
out= optimize.minimize(f, init, method='nelder-mead') 

it gives an error saying: f() missing 1 required positional argument: 'b'

How can i make it run.


Solution

  • what about this:

    def f(a):
        func = 0
        for i in range(10):
            func+= (a*i - b[i])**2
        return func
    init = [1]
    out= optimize.minimize(f, init, method='nelder-mead') 
    

    as you said your target variable is a and you have values for b, then you need to construct Obj function with one argument I guess.