Search code examples
pythonscipyminimumscipy-optimize

Finding the x that makes the function minimum python


enter image description hereI have a function and I am trying to find the minimum. here is my code:

import nyumpy as np 
from scipy.optimize import basinhopping, Bounds
from scipy import optimize
x=np.arange(-180.0,180.0)
bounds = Bounds(-180., 180.)
def P1_adj(x):
    return 60450.64625041*np.exp(1j*(57.29*1.75056445+x))

print(optimize.minimize(P1_adj, x0=0))

When I run it, I get this error

TypeError: '<' not supported between instances of 'complex' and 'float'

Would you please help me to understand what I am doing wrong? I need to find the x(angle) that makes this function minimum.


Solution

    • optimize.minimize does not support complex numbers, funct P1_adj returns a complex number, which is causing the error.
    import numpy as np
    from scipy.optimize import minimize
    from scipy import optimize
    
    def P1_adj(x):
        return np.abs(60450.64625041 * np.exp(1j * (57.29 * 1.75056445 + x)))
    
    result = minimize(P1_adj, x0=0)
    print(result)
    
    • here P1_adj funct calculates the absolute value of the complex number instead of returning the complex number itself