Matlab编码器fzero功能

时间:2016-03-31 17:16:17

标签: matlab function anonymous-function matlab-coder

我正在尝试使用“MATLAB Coder”将MATLAB代码转换为C代码,但不允许使用匿名函数。

如何将fzero函数转换为

myfun = @(x,c) cos(c*x);  % parameterized function
c = 2;                    % parameter
fun = @(x) myfun(x,c);    % function of x alone
x = fzero(fun,0.1)

进入正常函数,例如,将整个代码转换为C。

1 个答案:

答案 0 :(得分:1)

你有"匿名"功能,而不是" undefined"功能,只是为了清理术语。

将以下内容转换为命名函数:

myfun = @(x,c) cos(c*x);  % parameterized function

写下这个:

function result = myfun(x,c)

result = cos(c*x);

end

对于第二个函数,请写下:

function result = myfun2(x)
c = 2;
result = cos(c*x);

end

最后,像这样打电话给fzero:

x = fzero(@myfun2, 0.1);