how use fzero with matlab coder and a function with parameters ?
2 views (last 30 days)
I want to solve the equation f(x,p) = 0 for several values of p.
A correct way to perform this task is to write a function (here called findzero):
function y = findzero(p)
n = length(p);
y = zeros(1,n);
hf = @f;
y(il) = fzero(@(x) hf(x,p(il)),0);
out = x-p;
unfortunately, when I try to generate the C code of findzero with matlab coder, I encounter the following error : findzero, line 6, "anonymous functions are not supported for code generation"
How can I modify findzero in order to successfully pass it in matlab coder ?
Ryan Livingston on 24 Jun 2016
The typical workaround is to model an anonymous function using a sub-function with persistent variables to store the extra parameters. In your case, this function, let's reuse your f, would have the persistent p. Then, you make a call to f with 2 arguments to "set" the parameter right before the call to fzero:
% Set parameter value
% Find root
This answer gives an example in more detail which shows how to implement f.