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!