problems when using scipy.opti​mize.diffe​rential_ev​olution in Matlab environment

5 次查看(过去 30 天)
When i call a matlab function to be optimized with the inputs arguments i get 'not enough input' error.
For example; i have a matlab function called Residual.m that requires 5 input and returns one value.
Residual(state, N, freq, SNR, p)
My code is like this:
py_optimize = py.importlib.import_module('scipy.optimize');
N=2
bound = [-50, 500; -50, 500; -150, 150];
x_bound = repmat(bound, N, 1);
freq=10; SNR=30;
p=1;
data=py_optimize.differential_evolution(Residual,x_bound,disp=True, maxiter=4500, popsize=65, init='random', workers=worker, recombination=0.8, mutation=nn, strategy='best1bin', tol=0.001, updating='deferred');
When i run this, i get an error that there is not enough inputs.
I tried using matlabFunction with my fuction but i keep getting below error
Error using indexing
Handle to MATLAB function '@()' is not supported. Use a handle to a Python function.
What I'm i not doing right?

回答(1 个)

Al Danial
Al Danial 2023-6-4
scipy.optimize.differential_evolution() expects a Python function as its first argument. You'll need to translate Residual.m to Python, then pass this translated function as the first argument. I suppose in theory you could provide a Python wrapper to Residual.m then use the matlab.engine from Python to call Residual.m but I've not been successful with that combination.

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by