How to deal with matlab functions that are unsupported when using Matlab Coder? (How can I transfer Matlab intrinsic function 'quadprog()' into C code using Matlab Coder?)
Ältere Kommentare anzeigen
I write a matlab function to realize some algorithm. When I use Matlab Coder to convert it into C code, it build failed with the following error:
The function 'quadprog' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
error code is: Optimal=quadprog(H,f);
In fact, I want to know how to deal with matlab functions that are unsupported when using Matlab Coder.
Thanks for helping me.
Akzeptierte Antwort
Weitere Antworten (2)
Mary Fenelon
am 21 Apr. 2020
4 Stimmen
Code generation for quadprog is supported as of R2020a. Support for fmincon was added in R2019b.
4 Kommentare
CHANGPING ZHOU
am 15 Mai 2020
Thanks for telling us that Support for fmincon was added in R2019b. I install the R2020a version and I try to call the fmincon function in the MATLAB Fcn block. It runs sucessful but the output of the fmincon is always 0. There is no problem in the main program fmincon(@fcn) beacuse I can call @fcn in a .m file and it returns the right result. I have no idea where is the problem. Thanks for helping me.
The following picture is a part of the simulink model and I use the MATLAB Fcn to call the fmincon function. You can see the output are all zeros.

The following is the MATLAB Fcn program and I use fmincon to call MPCV3.m where u1,u2,u3 is the parameters. MPCV3.m is OK because I can call it in the other .m file and it returns the right result.
function [y1,y2] = fcn(u1,u2,u3)
Nc=4;
x = zeros(2*Nc,1);
y1=0;y2=0;
U0=zeros(2*Nc,1);
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
x = fmincon(@(U)(MPCV3(U,u1,u2,u3)),U0,[],[],[],[],[],[],@unc,options);
y1 = x(1,1);
y2 = x(2,1);
Partha Mishra
am 14 Sep. 2020
Is there a solution to this problem yet? I am facing the same problem. I am running a plant controller model that uses a fmincon-based optimization script inside the controller. If I include the optimization script in a MATLAB function script and declare that function as an extrinsic function, i.e. coder.extrinsic('MyOptimizationFunction.m'), I get good results and all the exitflag values at each time step is 1 (i.e. local optimum values found).
However, if I copy/paste the contents of "MyOptimizationFunction.m" into the MATLAB function block's function, it doesn't converge. All my exitflags are 2 or -2.
I am using fixed step, discrete solver in both these cases. The only change in these cases are running the optimization routine either in an extrinsic function or directly in a MATLAB function block. Any suggestions?
Adam Hug
am 15 Sep. 2020
Hello Zhou and Partha,
I understand that you are both experiencing issues migrating generated C code from a MATLAB environment to Simulink. My experience with these kinds of issues has found that the cause is usually numerical issues with the nonlinear objective or constraints. The problem is correctly modelled in exact arithmetic, but the solver doesn’t perform well when using floating point math. Because the MATLAB and code generated versions of fmincon don’t perform arithmetic in exactly the same order, floating point error is the most likely cause for the differences you are experiencing.
To mitigate these issues, there are a few things you can try:
- Double check that your objective and constraints are twice differentiable in the feasible region. Fmincon assumes that the path it takes towards the solution is smooth. If this is not the case, the math that guarantees both convergence and optimality will break down. Something as simple as a tan() or abs() function can throw off the solver.
- Use exact gradients or adjust the finite difference step size. It may be the case that your problem is “stiff”. Such a problem has gradients that may be inadequately modelled by forward or central finite differences. Using exact gradients is the most robust solution for these types of issues. If exact gradients are too time consuming to compute (and the model is not being deployed externally), you could try adjusting the step size of the finite difference approximation. This involves manipulating the "FiniteDifferenceStepSize" or "TypicalX" values.
- Scale the problem. Fmincon has a “ScaleProblem” option that will adjust your problem internally in an attempt to improve numerical precision. Mileage will vary with this option, but it occasionally improves solution quality.
I hope at least one of these suggestions helps get you back on track. If not, could you post or attach your objective and constraints in MATLAB code? I may be able to help narrow down the issue further.
Regards,
Adam
yakun ma
am 23 Mai 2022
When H maxtrix is empty, the quadProg is not supported for code generation. How could we handle this case? My problem is a linear programming. But linprog is not supported for code generation. So I want to use quadProg instead.
Fred Smith
am 10 Jan. 2013
2 Stimmen
If you don't really need C code but are just running the function in MATLAB , you can often use CODER.EXTRINSIC to call the original MATLAB version. This probably won't work for QUADPROG since it probably takes a function handle as an argument. Function handles are not supported in extrinsic calls but almost everything else is.
If you really need C code because you are running in an environment that MATLAB natively does not support, then you can either write your own MATLAB implementation of the missing functionality, or use coder.ceval to bring in external C code that implements the functionality.
If you don't need C code and but do need to deploy your algorithm on a MATLAB-supported host, you can use MATLAB Compiler.
Good luck.
3 Kommentare
dou
am 11 Jan. 2013
Bill Chou
am 1 Jun. 2016
For those interested, you may want to take a look at the webinar that talks about using MATLAB Coder to generate C code, then manually integrating it into an Android app via the JNI interface:
Nurul Farhana Mohd Fadzli
am 6 Jan. 2023
What about function minFeretProperties?
Kategorien
Mehr zu Quadratic Programming and Cone Programming finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!