How can I use a C++ function as the objective function of my FMINCON function in the Optimization Toolbox?

3 views (last 30 days)
I have a C++ function and I want to use it as an argument to MATLAB's FMINCON function. I would like to know the best way to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Feb 2017
The best way to achieve the functionality that you are looking for is by wrapping the objective function in a MEX file and then passing this MEX file as an argument to the FMINCON function. You can create a MEX wrapper function by closely following the example given at the site below:
Create C Source MEX file:
https://in.mathworks.com/help/matlab/matlab_external/standalone-example.html
Once the objective function is compiled using the MEX command, obtain a handle to your objective function as shown below:
fhandle = @my_obj_mex_func;
This function handle can then be given as the first argument to the FMINCON function as shown in the example below:
x = fmincon(@my_obj_mex_func,x0,A,b);

More Answers (0)

Categories

Find more on MATLAB Compiler SDK in Help Center and File Exchange

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!