fzero raises error before setting exitflag

1 view (last 30 days)
Fitzgerald
Fitzgerald on 21 Jan 2013
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!

Answers (1)

Walter Roberson
Walter Roberson on 21 Jan 2013
Giving endpoints where the function values do not differ in sign is a "true error" for fzero() purposes.

Categories

Find more on Optimization in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!