필터 지우기
필터 지우기

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

조회 수: 13 (최근 30일)
yechan jang
yechan jang 2023년 6월 13일
답변: Lakshay Rose 2023년 6월 14일
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
Angelo Yeo
Angelo Yeo 2023년 6월 14일
Can you share the error message if possible? Or can you elaborate what you mean by "not working"?

댓글을 달려면 로그인하십시오.

답변 (2개)

Seyeong Jeon
Seyeong Jeon 2023년 6월 14일
편집: Seyeong Jeon 2023년 6월 14일
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 2023년 6월 14일
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 –

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by