%% Roots of Scalar Functions
%% Solving a Nonlinear Equation in One Variable
% The fzero function attempts to find a root of one equation with one variable. You can call this function with either a one-element starting point or a two-element vector that designates a starting interval. If you give fzero a starting point x0, fzero first searches for an interval around this point where the function changes sign. If the interval is found, fzero returns a value near where the function changes sign. If no such interval is found, fzero returns NaN. Alternatively, if you know two points where the function value differs in sign, you can specify this starting interval using a two-element vector; fzero is guaranteed to narrow down the interval and return a value near a sign change.
% The following sections contain two examples that illustrate how to find a zero of a function using a starting interval and a starting point. The examples use the function humps.m, which is provided with MATLAB®. The following figure shows the graph of humps.

x = -1:.01:2;
y = humps(x);
plot(x,y)
xlabel('x');
ylabel('humps(x)')
grid on

%
%% Setting Options For fzero
% You can control several aspects of the fzero function by setting options. You set options using optimset. Options include:
% Choosing the amount of display fzero generates — see Set Options, Using a Starting Interval, and Using a Starting Point.
% Choosing various tolerances that control how fzero determines it is at a root — see Set Options.
% Choosing a plot function for observing the progress of fzero towards a root — see Plot Functions.
% Using a custom-programmed output function for observing the progress of fzero towards a root — see Output Functions.
%
%% Using a Starting Interval
% The graph of humps indicates that the function is negative at x = -1 and positive at x = 1. You can confirm this by calculating humps at these two points.

humps(1)
humps(-1)

% Consequently, you can use [-1 1] as a starting interval for fzero.
% The iterative algorithm for fzero finds smaller and smaller subintervals of [-1 1]. For each subinterval, the sign of humps differs at the two endpoints. As the endpoints of the subintervals get closer and closer, they converge to zero for humps.
% To show the progress of fzero at each iteration, set the Display option to iter using the optimset function.

options = optimset('Display','iter');

% Then call fzero as follows:

a = fzero(@humps,[-1 1],options)

% Each value x represents the best endpoint so far. The Procedure column tells you whether each step of the algorithm uses bisection or interpolation.
% You can verify that the function value at a is close to zero by entering

humps(a)

%
%% Using a Starting Point
% Suppose you do not know two points at which the function values of humps differ in sign. In that case, you can choose a scalar x0 as the starting point for fzero. fzero first searches for an interval around this point on which the function changes sign. If fzero finds such an interval, it proceeds with the algorithm described in the previous section. If no such interval is found, fzero returns NaN.
% For example, set the starting point to -0.2, the Display option to Iter, and call fzero:

options = optimset('Display','iter');
a = fzero(@humps,-0.2,options)

% The endpoints of the current subinterval at each iteration are listed under the headings a and b, while the corresponding values of humps at the endpoints are listed under f(a) and f(b), respectively.
% Note: The endpoints a and b are not listed in any specific order: a can be greater than b or less than b.
% For the first nine steps, the sign of humps is negative at both endpoints of the current subinterval, which is shown in the output. At the tenth step, the sign of humps is positive at a, -0.10949, but negative at b, -0.264. From this point on, the algorithm continues to narrow down the interval [-0.10949 -0.264], as described in the previous section, until it reaches the value -0.1316.
% Copyright 2015-2019 The MathWorks, Inc.