Root of nonlinear functioncollapse all in page
Syntax
x = fzero(fun,x0)example
x = fzero(fun,x0,options)example
x = fzero(problem)example
[x,fval,exitflag,output] = fzero(___)example
Description
Root of nonlinear functioncollapse all in page
Syntax
x = fzero(fun,x0)example
x = fzero(fun,x0,options)example
x = fzero(problem)example
[x,fval,exitflag,output] = fzero(___)example
Description
example
x = fzero(fun,x0) tries to find a point x where fun(x) = 0. This solution is where fun(x) changes sign—fzero cannot find a root of a function such as x^2.
example
x = fzero(fun,x0,options) uses options to modify the solution process.
example
x = fzero(problem) solves a root-finding problem specified by problem.
example
[x,fval,exitflag,output] = fzero(___) returns fun(x) in the fval output, exitflag encoding the reason fzero stopped, and an output structure containing information on the solution process.
Examples
collapse all
Root Starting From One Point
Open This Example
Calculate $\pi$ by finding the zero of the sine function near 3.
fun = @sin; % function
x0 = 3; % initial point
x = fzero(fun,x0)
x =
3.1416
Root Starting From an Interval
Open This Example
Find the zero of cosine between 1 and 2.
fun = @cos; % function
x0 = [1 2]; % initial interval
x = fzero(fun,x0)
x =
1.5708
Note that $\cos(1)$ and $\cos(2)$ differ in sign.
Root of a Function Defined by a File
Find a zero of the function f(x) = x3 – 2x – 5.
First, write a file called f.m.
function y = f(x)
y = x.^3 - 2*x - 5;
Save f.m on your MATLAB® path.
Find the zero of f(x) near 2.
fun = @f; % function
x0 = 2; % initial point
z = fzero(fun,x0)
z =
2.0946
Since f(x) is a polynomial, you can find the same real zero, and a complex conjugate pair of zeros, using the roots command.
roots([1 0 -2 -5])
ans =
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
Root of Function with Extra Parameter
Open This Example
Find the root of a function that has an extra parameter.
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)
x =
0.7854
Nondefault Options
Open This Example
Plot the solution process by setting some plot functions.
Define the function and initial point.
fun = @(x)sin(cosh(x));
x0 = 1;
Examine the solution process by setting options that include plot functions.
options = optimset('PlotFcns',{@optimplotx,@optimplotfval});
Run fzero including options.
x = fzero(fun,x0,options)
x =
1.8115
Solve Problem Structure
Open This Example
Solve a problem that is defined by a problem structure.
Define a structure that encodes a root-finding problem.
problem.objective = @(x)sin(cosh(x));
problem.x0 = 1;
problem.solver = 'fzero'; % a required part of the structure
problem.options = optimset(@fzero); % default options
Solve the problem.
x = fzero(problem)
x =
1.8115
More Information from Solution
Open This Example
Find the point where exp(-exp(-x)) = x, and display information about the solution process.
fun = @(x) exp(-exp(-x)) - x; % function
x0 = [0 1]; % initial interval
options = optimset('Display','iter'); % show iterations
[x fval exitflag output] = fzero(fun,x0,options)
Func-count x f(x) Procedure
2 1 -0.307799 initial
3 0.544459 0.0153522 interpolation
4 0.566101 0.00070708 interpolation
5 0.567143 -1.40255e-08 interpolation
6 0.567143 1.50013e-12 interpolation
7 0.567143 0 interpolation
Zero found in the interval [0, 1]
x =
0.5671
fval =
0
exitflag =
1
output =
intervaliterations: 0
iterations: 5
funcCount: 7
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [0, 1]'
fval = 0 means fun(x) = 0, as desired.
Related Examples
Roots of Scalar Functions
Parameterizing Functions
Input Arguments
collapse all
fun — Function to solve
function handle
Function to solve, specified as a handle to a scalar-valued function. fun accepts a scalar x and returns a scalar fun(x).
fzero solves fun(x) = 0. To solve an equation fun(x) = c(x), instead solve fun2(x) = fun(x) - c(x) = 0.
To include extra parameters in your function, see the example Root of Function with Extra Parameter and the section Parameterizing Functions.
Example: @sin
Example: @myFunction
Example: @(x)(x-a)^5 - 3*x + a - 1
Data Types: function_handle
x0 — Initial value
scalar | 2-element vector
Initial value, specified as a real scalar or a 2-element real vector.
Scalar — fzero begins at x0 and tries to locate a point x1 where fun(x1) has the opposite sign of fun(x0). Then fzero iteratively shrinks the interval where fun changes sign to reach a solution.
2-element vector — fzero checks that fun(x0(1)) and fun(x0(2)) have opposite signs, and errors if they do not. It then iteratively shrinks the interval where fun changes sign to reach a solution. An interval x0 must be finite; it cannot contain ±Inf.
Tip Calling fzero with an interval (x0 with two elements) is often faster than calling it with a scalar x0.
Example: 3
Example: [2,17]
Data Types: double
options — Options for solution process
structure, typically created using optimset
Options for solution process, specified as a structure. Create or modify the options structure using optimset. fzero uses these options structure fields.
Display
Level of display:
'off' displays no output.
'iter' displays output at each iteration.
'final' displays just the final output.
'notify' (default) displays output only if the function does not converge.
FunValCheck
Check whether objective function values are valid.
'on' displays an error when the objective function returns a value that is complex, Inf, or NaN.
The default, 'off', displays no error.
OutputFcn
Specify one or more user-defined functions that an optimization function calls at each iteration, either as a function handle or as a cell array of function handles. The default is none ([]). See Output Functions.
PlotFcns
Plot various measures of progress while the algorithm executes. Select from predefined plots or write your own. Pass a function handle or a cell array of function handles. The default is none ([]).
@optimplotx plots the current point.
@optimplotfval plots the function value.
For information on writing a custom plot function, see Plot Functions.
TolX
Termination tolerance on x, a positive scalar. The default is eps, 2.2204e–16.
Example: options = optimset('FunValCheck','on')
Data Types: struct
problem — Root-finding problem
structure
Root-finding problem, specified as a structure with all of the following fields.
objective
Objective function
x0
Initial point for x, real scalar or 2-element vector
solver
'fzero'
options
Options structure, typically created using optimset
For an example, see Solve Problem Structure.
Data Types: struct
Output Arguments
collapse all
x — Location of root or sign change
real scalar
Location of root or sign change, returned as a scalar.
fval — Function value at x
real scalar
Function value at x, returned as a scalar.
exitflag — Integer encoding the exit condition
integer
Integer encoding the exit condition, meaning the reason fzero stopped its iterations.
1
Function converged to a solution x.
-1
Algorithm was terminated by the output function or plot function.
-3
NaN or Inf function value was encountered while searching for an interval containing a sign change.
-4
Complex function value was encountered while searching for an interval containing a sign change.
-5
Algorithm might have converged to a singular point.
-6
fzero did not detect a sign change.
output — Information about root-finding process
structure
Information about root-finding process, returned as a structure. The fields of the structure are:
intervaliterations
Number of iterations taken to find an interval containing a root
iterations
Number of zero-finding iterations
funcCount
Number of function evaluations
algorithm
'bisection, interpolation'
message
Exit message
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)
ans =
16
humps(-1)
ans =
-5.1378
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)
Func-count x f(x) Procedure
2 -1 -5.13779 initial
3 -0.513876 -4.02235 interpolation
4 -0.513876 -4.02235 bisection
5 -0.473635 -3.83767 interpolation
6 -0.115287 0.414441 bisection
7 -0.115287 0.414441 interpolation
8 -0.132562 -0.0226907 interpolation
9 -0.131666 -0.0011492 interpolation
10 -0.131618 1.88371e-07 interpolation
11 -0.131618 -2.7935e-11 interpolation
12 -0.131618 8.88178e-16 interpolation
13 -0.131618 8.88178e-16 interpolation
Zero found in the interval [-1, 1]
a =
-0.1316
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)
ans =
8.8818e-16
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)
Search for an interval around -0.2 containing a sign change:
Func-count a f(a) b f(b) Procedure
1 -0.2 -1.35385 -0.2 -1.35385 initial interval
3 -0.194343 -1.26077 -0.205657 -1.44411 search
5 -0.192 -1.22137 -0.208 -1.4807 search
7 -0.188686 -1.16477 -0.211314 -1.53167 search
9 -0.184 -1.08293 -0.216 -1.60224 search
11 -0.177373 -0.963455 -0.222627 -1.69911 search
13 -0.168 -0.786636 -0.232 -1.83055 search
15 -0.154745 -0.51962 -0.245255 -2.00602 search
17 -0.136 -0.104165 -0.264 -2.23521 search
18 -0.10949 0.572246 -0.264 -2.23521 search
Search for a zero in the interval [-0.10949, -0.264]:
Func-count x f(x) Procedure
18 -0.10949 0.572246 initial
19 -0.140984 -0.219277 interpolation
20 -0.132259 -0.0154224 interpolation
21 -0.131617 3.40729e-05 interpolation
22 -0.131618 -6.79505e-08 interpolation
23 -0.131618 -2.98428e-13 interpolation
24 -0.131618 8.88178e-16 interpolation
25 -0.131618 8.88178e-16 interpolation
Zero found in the interval [-0.10949, -0.264]
a =
-0.1316
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 the endpoint, -0.10949, but negative at the endpoint, -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.
Parameterizing Functions
Overview
This topic explains how to store or access extra parameters for mathematical functions that you pass to MATLAB® function functions, such as fzero or integral.
MATLAB function functions evaluate mathematical expressions over a range of values. They are called function functions because they are functions that accept a function handle (a pointer to a function) as an input. Each of these functions expects that your objective function has a specific number of input variables. For example, fzero and integral accept handles to functions that have exactly one input variable.
Suppose you want to find the zero of the cubic polynomial x3 + bx + c for different values of the coefficients b and c. Although you could create a function that accepts three input variables (x, b, and c), you cannot pass a function handle that requires all three of those inputs to fzero. However, you can take advantage of properties of anonymous or nested functions to define values for additional inputs.
Parameterizing Using Nested Functions
One approach for defining parameters is to use a nested function—a function completely contained within another function in a program file. For this example, create a file named findzero.m that contains a parent function findzero and a nested function poly:
function y = findzero(b,c,x0)
y = fzero(@poly,x0);
function y = poly(x)
y = x^3 + b*x + c;
end
end
The nested function defines the cubic polynomial with one input variable, x. The parent function accepts the parameters b and c as input values. The reason to nest poly within findzero is that nested functions share the workspace of their parent functions. Therefore, the poly function can access the values of b and c that you pass to findzero.
To find a zero of the polynomial with b = 2 and c = 3.5, using the starting point x0 = 0, you can call findzero from the command line:
x = findzero(2,3.5,0)
x =
-1.0945
Parameterizing Using Anonymous Functions
Another approach for accessing extra parameters is to use an anonymous function. Anonymous functions are functions that you can define in a single command, without creating a separate program file. They can use any variables that are available in the current workspace.
For example, create a handle to an anonymous function that describes the cubic polynomial, and find the zero:
b = 2;
c = 3.5;
cubicpoly = @(x) x^3 + b*x + c;
x = fzero(cubicpoly,0)
x =
-1.0945
Variable cubicpoly is a function handle for an anonymous function that has one input, x. Inputs for anonymous functions appear in parentheses immediately following the @ symbol that creates the function handle. Because b and c are in the workspace when you create cubicpoly, the anonymous function does not require inputs for those coefficients.
You do not need to create an intermediate variable, cubicpoly, for the anonymous function. Instead, you can include the entire definition of the function handle within the call to fzero:
b = 2;
c = 3.5;
x = fzero(@(x) x^3 + b*x + c,0)
x =
-1.0945
You also can use anonymous functions to call more complicated objective functions that you define in a function file. For example, suppose you have a file named cubicpoly.m with this function definition:
function y = cubicpoly(x,b,c)
y = x^3 + b*x + c;
end
At the command line, define b and c, and then call fzero with an anonymous function that invokes cubicpoly:
b = 2;
c = 3.5;
x = fzero(@(x) cubicpoly(x,b,c),0)
x =
-1.0945
Note: To change the values of the parameters, you must create a new anonymous function. For example:
b = 10;
c = 25;
x = fzero(@(x) x^3 + b*x + c,0);