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

16 次查看(过去 30 天)
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?

回答(3 个)

Steven Lord
Steven Lord 2022-6-27
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 个评论
Torsten
Torsten 2022-6-28
编辑:Torsten 2022-6-28
If an example from the MATLAB home page does not work, you should contact MATLAB support.
I suppose you have a codegen licence ?

请先登录,再进行评论。


Rik
Rik 2022-6-27
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 2022-8-15
you can check whether you have installed the Optimization Toolbox

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by