why I get the error "function 'lsqcurvefit' not supported for code generation" when using Matlab Coder?
显示 更早的评论
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?
1 个评论
Torsten
2022-6-27
Did you read this instruction page ?
回答(3 个)
Steven Lord
2022-6-27
1 个投票
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 个评论
Yongneng Liu
2022-6-28
Torsten
2022-6-28
First try if it works for the example provided under
before you use your own code.
Yongneng Liu
2022-6-28
Rik
2022-6-27
0 个投票
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.
1 个评论
Torsten
2022-6-27
lsqcurvefit and lsqnonlin are supported under certain conditions:
di
2022-8-15
0 个投票
you can check whether you have installed the Optimization Toolbox
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!