The solutions for this case of root-finding are typified by the use of Newton's method or the bisection method. However, in MATLAB, your first line of attack for robust root-finding should use the built-in function fzero. The on-line information is fairly extensive and the focus here is on outlining a simple method for using fzero that leads to a minimal number of problems and execution errors.
To use fzero, you need to know how to write functions in MATLAB.
As an illustration, consider the following problem:
Find the root(s) of f(x) = ae-bx - cx = 0 where, for example, a = 1, b = 2 and c = 3.
We'll go thru the basic use of fzero and then add on some refinements.
For the example problem, the Mfile would be
function y = rootex(x) %ROOTEX Example function for root-finding % Parameters a = 1; b = 2; c = 3; % Evaluate function: y = f(x). If x is a % root, y will be 0. y = a*exp(-b*x) - c*x;
(To get the most of of this procedure, you should start up MATLAB and create the function Mfile just given. If you open a new editor window, you can copy and paste from your browser into MATLAB to save some typing.)
Hint: If it is at all possible, make sure your function returns a
variable (y) that is the same size as the input
(x). While fzero does not require this,
the next step will be much easier if you do so.
The better the estimate of the interval that contains the root, the fewer problems you will have with fzero finding an answer.
For the example problem, the interval that contains the root will depend on the parameters (a, b and c). One way to make a reasonable estimate of the interval is to plot the function and look for places that the curve passes through zero. If your MATLAB function was written to return an vector when provided with a vector, use this feature to your advantage. For example,
xi = linspace(0,1); % intial guess for the interval y = rootex(x); % f(x) in the interval plot(x,y) % see how we did
(If you are running MATLAB, copy and paste the lines just given into
the command window to see the plot). The guess was "lucky" and indeed
the function changes sign in the interval. If the guess was not so
fortuitous, I would have changed the interval and tried again.
r = fzero(fname,[lo hi])
where fname is a string containing the name of the function Mfile (without the .m extension) and lo and hi define the interval which contains the root (which you should have determined in Step 2). [NB. The ability to specify a range is new for fzero in version 5.]
Note that the first argument (fname) must be (or contain) a string. There are two ways to ensure that this is so. For the example problem,
Version 1: Use a variable.
fname = 'rootex' % the function Mfile name r = fzero(fname,[0 1])
Version 2: Enter the name directly.
r = fzero('rootex',[0 1])
In the first version, a variable called fname was assigned the string 'rootex' while in the second, 'rootex' was used directly as an input.
When I ran this example (using either version), the root I found was
0.2163.
Controlling accuracy
There is an optional third input you can supply to fzero to set the accuracy of the final result. For instance, using the example problem from above,
r = fzero('rootex',[0 1],1e-10)
overrides the default accuracy (1e-6) and tries to get the root accurate
to 10 significant figures (roughly).
Changing parameters
Very often, the function you are seeking the roots of comes from a physical problem and so there may be many parameters that change when the basis of the problem changes. For example:
The syntax of fzero allows you to pass extra input arguments to your Mfile and hence releases you from the requirement that your function have a single input argument.
For the example problem, the Mfile can be modified so that the parametes are in the input argument list.
function y = rootex2(x,a,b,c) %ROOTEX2 Modified example function for root-finding % The parameters are in the input list % Evaluate function: y = f(x). If x is a % root, y will be 0. y = a*exp(-b*x) - c*x;
Then, fzero can be used as
r = fzero('rootex2',[0 1],1e-10,[],1,2,3)
to reproduce the first example (root at 0.2163) and as
r = fzero('rootex2',[0 1],1e-10,[],3,2,1)
to examine a different set of parameters (a = 3, b = 2 and c = 1, in this case which leads to a root of 0.7162).
Note: You must explicitly include the accuracy and the
(usually) optional fourth argument (which controls display of progress of
the operation) when you ask fzero to pass parameters to your
function.