Can i use fmincon in matlab using matlab api? if possible how?
11 ビュー (過去 30 日間)
古いコメントを表示
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
2023 年 6 月 14 日
Can you share the error message if possible? Or can you elaborate what you mean by "not working"?
回答 (2 件)
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([])
0 件のコメント
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 –
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!