Finding the x that makes the function minimum python

78 views Asked by At

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.

2

There are 2 answers

0
Sauron On BEST ANSWER
  • 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
2
Siong Thye Goh On

Notice your objective function

60450.64625041*np.exp(1j*(57.29*1.75056445+x))

1j here is a complex number, the solver doesn't know how to compare it with a typical float number. Convert it to a real number for it to work.