fzero raises error before setting exitflag
显示 更早的评论
I have a function fh of which I would like to calculate the zero within a certain interval. If there is no zero in the interval, then I'd like to do something else.
The following code does not work:
fh = @(x) x + 1;
try
[ds, ~, exitflag] = fzero(fh, [0 10]);
catch
end
switch exitflag
case 1
fprintf('found a solution\n')
case -6
fprintf('no solution in interval\n')
otherwise
error('something is wrong')
end
The problem is that fzero raises an error: "The function values at the interval endpoints must differ in sign," and doing so before setting the exitflag. I can catch the exception raised by fzero, but then there is no way to tell the difference between a true error or that there was just no zero in the specified interval.
How can I use exitflag, if the only possible value of exitflag is 1 (the case that no error is raised by fzero)?
Thanks!
回答(1 个)
Walter Roberson
2013-1-21
0 个投票
Giving endpoints where the function values do not differ in sign is a "true error" for fzero() purposes.
类别
在 帮助中心 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!