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?

Antworten (3)

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 Kommentare

Thanks for your advice. My version is R2022a, and now I have tried to specify the function using optimoptions. But I get two errors:
1.Function 'optimoptions' not supported for code generation.
2.Undefined function or variable 'opts'. The first assignment to a local variable determines its class.
As for error 2, I have tried to state the variable 'opts' as struct, but it did not work.
So what I can do to solve this problem?
fiiting2.m:
function a=fitting2(x,y)%#codegen
fun=@(a,x)a(1)*x.^2+a(2)*x;
a0=[2;2];
lb=[];
ub=[];
opts=optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIterations',1e4);
a=lsqcurvefit(fun,a0,x,y,lb,ub,opts);
end
test2.m:
clear
x=[1 2 3];
y=[5 8 9];
a=fitting2(x,y);
I have tried the example but it did not work. The error is as the same as the above error 1.
If an example from the MATLAB home page does not work, you should contact MATLAB support.
I suppose you have a codegen licence ?

Melden Sie sich an, um zu kommentieren.

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.
you can check whether you have installed the Optimization Toolbox

Gefragt:

am 27 Jun. 2022

Beantwortet:

di
am 15 Aug. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by