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?)

조회 수: 42 (최근 30일)
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.

채택된 답변

Kaustubha Govind
Kaustubha Govind 2013년 1월 9일
You can only generate standalone C/C++ code from functions supported for code-generation. You may either need to write your own C implementation for the function, or use MATLAB Compiler to deploy your MATLAB code. The code generated by MATLAB Compiler is not completely standalone (like that generated by MATLAB Coder), but depends on MATLAB run-time libraries, and therefore requires that each target machine either have the same version of MATLAB installed or have the corresponding version of the MATLAB Compiler Runtime installed.
  댓글 수: 1
dou
dou 2013년 1월 11일
Thanks for your help. In fact, I write the core algorithm in matlab. And now I want to add it into my Android application. I have tried to change my matlab code into Java class and add .jar file into my project. But when I run the Android project on VM. It stopped with class (made from matlab code) not found error.Now I want to transfer my matlab code into standalone C code. Then made it into .so file in order to be added into android application.

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

추가 답변 (2개)

Mary Fenelon
Mary Fenelon 2020년 4월 21일
Code generation for quadprog is supported as of R2020a. Support for fmincon was added in R2019b.
  댓글 수: 4
Adam Hug
Adam Hug 2020년 9월 15일
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:
  1. 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.
  2. 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.
  3. 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
yakun ma 2022년 5월 23일
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
Fred Smith 2013년 1월 10일
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
Bill Chou
Bill Chou 2016년 6월 1일
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:

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

카테고리

Help CenterFile Exchange에서 Android Devices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by