Can i use fmincon in matlab using matlab api? if possible how?
12 次查看(过去 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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1410334/image.png)
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!