Can i use fmincon in matlab using matlab api? if possible how?

3 vues (au cours des 30 derniers jours)
yechan jang
yechan jang le 13 Juin 2023
if it's possible how to brinf fmincon
I thick
solution = matlab_eng.fmincon(objective, x0, [], [], [], [], lb, ub)
is not working
%%%code is written in python
import matlab.engine
# Start the MATLAB Engine
matlab_eng = matlab.engine.start_matlab()
# Define the objective function
def objective(x):
return x**2
# Define the initial guess
x0 = matlab.double([2.0])
# Define the lower and upper bounds for the variable
lb = matlab.double([0.0])
ub = matlab.double([5.0])
# Solve the optimization problem using fmincon
solution = matlab_eng.fmincon(objective, x0, [], [], [], [], lb, ub)
# Print the results
print("Optimization Results:")
print("Objective Value:", solution)
# Stop the MATLAB Engine
matlab_eng.quit()
  1 commentaire
Angelo Yeo
Angelo Yeo le 14 Juin 2023
Can you share the error message if possible? Or can you elaborate what you mean by "not working"?

Connectez-vous pour commenter.

Réponses (2)

Seyeong Jeon
Seyeong Jeon le 14 Juin 2023
Modifié(e) : Seyeong Jeon le 14 Juin 2023
Corrected Code:
%%% code is written in python
import matlab.engine
# Start the MATLAB Engine
eng = matlab.engine.start_matlab()
# Define the objective function
eng.eval('objective = @(x) x.^2', nargout=0)
# Define the initial guess
x0 = matlab.double([2.0])
# Define the lower and upper bounds for the variable
lb = matlab.double([0.0])
ub = matlab.double([5.0])
# Define blank variable
blank = matlab.double([])
# Solve the optimization problem using fmincon
solution = eng.fmincon(eng.workspace['objective'], x0, blank, blank, blank, blank, lb, ub)
# Print the results
print("Optimization Results:")
print("Objective Value:", solution)
# Stop the MATLAB Engine
eng.quit()
Execution Result :
The problem of your code was that MATLAB engine(eng.fmincon) cannot read python function.
Thus, if you put the function as Matlab function, the fmincon function operates successfully.
you can see the objective value calculated correctly.
Also, you must repalce [] with matlab.double([])

Lakshay Rose
Lakshay Rose le 14 Juin 2023
Hello yechan jang,
As per my understanding, you want to use “fmincon” function from the “matlab.engine” in python.
Based on the provided code, the error occurs in this line
solution = matlab_eng.fmincon(objective, x0, [], [], [], [], lb, ub)
The error occurs because the “fmincon” function accepts the argument as MATLAB functions only.
To resolve this error, you can convert the python function into a MATLAB function using the below code –
# Define the objective function
matlab_eng.eval('objective = @(x) x.^2', nargout=0)
# Solve the optimization problem using fmincon
solution = matlab_eng.fmincon(eng.workspace['objective'], x0, [], [], [], [], lb, ub)
You can refer to the below documentation to learn about the arguments of the “fmincon” function in MATLAB –

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by