Search code examples
pythonscipy-optimize-minimize

Exclude some arguments from the minimization


If I have a function func(x1,x2,x3), can I use the minimize function scipy.optimize.minimize with excluding x3 from the optimization process, where x3 is defined as numpy array. How can I define the argument in this case?. I'm supposed to get an array containing the minimum values of func for each value of x3.

For example:

def func(thet1,phai1,thet2,phai2,c2):
    
    RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])   
    w, v = np.linalg.eig(RhoABC)  
    return w[1] 

I want to minimize it where c2 = linspace(-1,1,10) and the angles belong to (0,2pi)


Solution

  • Maybe you could use something like this:

    def func(thet1,phai1,thet2,phai2,*args, c2 = []):
    #considering c2 to be x3 in the above post
        
        RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])   
        w, v = np.linalg.eig(RhoABC)  
        return w[1] 
    

    Then when you call the function:

    retVal = func(thet1,phai1,thet2,phai2, c2=c2)
    
    #you have to specify c2 first and then equate it to the value since this is an optional argument.