why I get the error "function 'lsqcurvefit' not supported for code generation" when using Matlab Coder?

16 views (last 30 days)
I want to convert the function to the C code. And the following is the function fitting2.m and test2.m.
fiiting2.m:
function a=fitting2(x,y)
fun=@(a,x)a(1)*x.^2+a(2)*x;
a0=[2;2];
lb=[];
ub=[];
a=lsqcurvefit(fun,a0,x,y,lb,ub);
end
test2.m:
clear
x=[1 2 3];
y=[5 8 9];
a=fitting2(x,y);
My objection is to covert the function lsqcurvefit to C code, so my code is simple, but I always get the error, So what's the problem?

Answers (3)

Steven Lord
Steven Lord on 27 Jun 2022
Not all functions in Optimization Toolbox support being converted to C or C++ code using MATLAB Coder. In the most recent release this is the list of functions that do have that support, though some of them may have restrictions or limitations (certain options not being supported, for example.)
I don't know exactly when lsqcurvefit introduced support for use with MATLAB Coder or what release you're using, but looking at the Extended Capabilities section of the lsqcurvefit function documentation page I noticed:
"You must include options for lsqcurvefit or lsqnonlin and specify them using optimoptions. The options must include the Algorithm option, set to 'levenberg-marquardt'."
You aren't specifying any options.
Because of that entry on the documentation page I suspect this support was added in release R2020b as per the Release Notes stating that code generation support for Levenberg-Marquardt was added in that release. This is supported by the fact that the documentation page for this function in release R2020a does not list C/C++ code generation support in the Extended Capabilities section.
What release are you using?
  4 Comments
Torsten
Torsten on 28 Jun 2022
Edited: Torsten on 28 Jun 2022
If an example from the MATLAB home page does not work, you should contact MATLAB support.
I suppose you have a codegen licence ?

Sign in to comment.


Rik
Rik on 27 Jun 2022
Your code may be simple, but the code inside lsqcurvefit apparently is not. There are a lot of things happening in that function, and apparently not everything can be converted to C. Not all functions are supported.
You will have to replicate the required functionality with functions that are supported for C generation. Those functions will mention support in the documention.
Alternatively, you may want to consider why exactly you want to convert your Matlab code to C/C++. There may be alternatives.

di
di on 15 Aug 2022
you can check whether you have installed the Optimization Toolbox

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!