Search code examples
pythonscipymathematical-optimizationscipy-optimize

Problems minimizing a two variable function with "scipy.optimize.brute"


I'm trying to minimize a function of two variables using scipy.optimize.brute algorithm, but I've receiving the error TypeError: Fcone() missing 1 required positional argument: 'R'.

Here's my code:

import numpy as np
import scipy.optimize as opt

gamma = 17.
C = 45.
T = 12.
H = 1.2
R = 1.3
H1 = 0.57

def Fcone(alpha, beta, H1, gamma, C, T, H, R):
    return np.pi/((np.cos(alpha))**2 * (np.cos(beta))**2) * ((np.cos(alpha))**2 
             * ((np.cos(beta))**2 * (gamma*H1**3 + 2*H1**2*(C-T-gamma*H) - H*H1*
                    (2*C-2*T-gamma*H) + H*(gamma*R**2 + H*(C-T)-(gamma*H**2)/3)) -
                2*R*(H-H1)*np.cos(beta) * ((gamma*H1/2 - gamma*H/2 + C -T)*
                    np.sin(beta) - C) + (C*np.sin(beta) + gamma*H/3 - gamma*H1/3 
                        -C + T)*(H-H1)**2) - 2*H1*np.cos(beta)*np.cos(alpha)*(
                        R*np.cos(beta)*((gamma*H1/2 + C -T -gamma*H)*np.sin(alpha)
                    - C) + np.sin(alpha)*(H-H1)*((gamma*H1/2 - gamma*H/2 + C -T)
                                *np.sin(beta)-C)) + (np.cos(beta))**2 * H1**2 *
                                                 (gamma*H + C*np.sin(alpha) 
                                                  -2*gamma*H1/3 -C+T))

ranges = (slice(0, np.pi/2, np.pi/10000), slice(0, np.pi/2, np.pi/10000))
res = opt.brute(Fcone, ranges, args=(H1, gamma, C, T, H, R), finish=None)

Where gamma, C,T, H, R1 and H1 are given parameters. Despite the very large (and probably confusing equation), how can I fix this problem and minimize the function for the two parameters alpha and beta? Thank you!

P.S.: The function alone is running fine when I assign all values for the parameters.


Solution

  • Amend Fcone() as follows...

    def Fcone(alpha_beta, H1, gamma, C, T, H, R):
        alpha = alpha_beta[0]
        beta  = alpha_beta[1]
        .....
    

    The variables being optimized have to be put into a 1D array, check docs here. Their values will be in res[0]