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!