I'm trying to use MATLAB with Python and would like to use MATLAB's fmincon to optimize a Python function. Here's an example of what I'm trying to achieve (in Python).
import matlab.engine
from scipy import stats
m = matlab.engine.start_matlab()
def my_fun(x):
return -stats.norm.pdf(x, 0, 1)
x0 = matlab.double([-1, 2])
A = matlab.double([1, 2])
b = 1
x = m.fmincon(my_fun, x0, A, b, nargout=2)
print(x)
m.exit()
However, I get the following error:
TypeError: unsupported Python data type: function
I then tried calling Python from MATLAB from Python (!) in this way:
x = m.fmincon(m.py.my_fun, x0, A, b, nargout=2)
But I got this error:
TypeError: unsupported Python data type: matlab.engine.matlabengine.MatlabFunc
Is there any different workaround? I think the second thing I tried should work in principle, but the way I implemented it is wrong.