Using Matlab coder with lsqcurvefit(), can not pass optimoptions as an argument to the target function
Show older comments
I want to convert my function to mex using matlab coder. The function fits data to an exponential model.
function ab_fitted = my_fit_ab_1(x0, xdata, ydata)
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIter', 100);
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
The script main_1.m that calls my_fit_ab_1() is as follows,
clear
a = rand();
b = rand();
xdata = rand(8,1);
ydata = a*exp(-b*xdata);
x0 = [rand, rand];
ab_fitted = my_fit_ab_1(x0, xdata, ydata);
With matlab coder App, I can easily convert my_fit_ab_1() to mex file my_fit_ab_1_mex.mexw64.
However, if I want to pass the variable Opt as an argument, i.e. my_fit_ab_2.m
function ab_fitted = my_fit_ab_2(x0, xdata, ydata, Opt)
simple_exp_func = @(x, xdata) x(1)*exp(-x(2)*xdata); % y=a*exp(-b)
ab_fitted = lsqcurvefit(simple_exp_func, x0, xdata, ydata, [],[], Opt);
end
and main_2.m
clear
a = rand();
b = rand();
xdata = rand(8,1);
ydata = a*exp(-b*xdata);
Opt = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxIter', 100);
x0 = [rand, rand];
ab_fitted = my_fit_ab_2(x0, xdata, ydata, Opt);
the coder says " The 'optim.options.Lsqcurvefit' class does not support code generation. "
Why optimoptions can be set inside the function while can not be passed as an argument?
I've attached the four files mentioned above.
Accepted Answer
More Answers (0)
Categories
Find more on Automated Fixed-Point Conversion in MATLAB 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!