How to Implement the Bisection Root-Finding Method in MATLAB?

In summary, The bisection algorithm is used to find a root of a function. The function must be of a single variable and must return a scalar result. The approximation loop is controlled by three termination criteria: - estimate of the approximation error is less than errTol - number of iterations is greater than iterMax - function value magnitude is less than funTol. The initial bracket controls the search. The initial bracket must be defined such that the sign of the function is opposite at each end of the bracket (to ensure a zero crossing exists between the two. THERE IS NO ERROR CHECKING for this in the current code). The inputs and outputs are as follows: @fun = function that is the subject
  • #1
dillonmhudson
49
0

Homework Statement


Create and test a MATLAB function called bisection that implements the bisection root finding method. You will have to use a function handle and exit flag coding.

Homework Equations


-

The Attempt at a Solution



Code:
% bisection.m

function [root, err, iter, exitFlag] = bisection (fun, lb, ub, errMax, iterMax)

% This function performs the bisection algorithm on a function to
% locate a root of the function (location where it crosses zero). The
% function must be of a single variable and must return a scalar result
% (i.e., a scalar function of a scalar argument). 
% 
% The approximation loop is controlled by three termination criteria:
%  - estimate of the approximation error is less than errTol
%  - number of iterations is greater than iterMax
%  - function value magnitude is less than funTol
%
% The initial bracket controls the search. The initial bracket must be
% defined such that the sign of the function is opposite at each end of the
% bracket (to ensure a zero crossing exists between the two. THERE IS NO
% ERROR CHECKING for this in the current code.
%
% Inputs:  @fun = function that is the subject of the root-finding
%          lb = lower bound of the bisection search bracket
%          ub = upper bound of the bisection search bracket
%          errMax = maximum acceptable relative approximation error
%          iterMax = maximum number of iterations
%
% Outputs: root = approximation of root location
%          err = relative approximation error of solution
%          iter = number of iterations taken to find the solution
%          exitFlag = indicates termination status of function
% 
% Exit Flag Encoding: 1: Alrogithm terminated normally (due to error being
%                        sufficiently small)
%                     0: Alrogithm terminated due to maximum number of
%                        iterations being reached
%                    -1: Algorithm terminated due to invalid bracket 
%                        specification (no root in bracket)
%                    -2: Algorithm terminated due to invalid return value
%                        from function fun (e.g., NaN, Inf, empty brakets)

%%%%%%%%%%%%%%%%%%%%%%%%%%
% GENERAL INITIALIZATION %
%%%%%%%%%%%%%%%%%%%%%%%%%%
iter = 0;       % iteration counter variable
done = false;   % Boolean variable to control looping
root = (ub+lb)/2; % initial approximation of root location 
err = 100*abs([ub-lb]/[ub+lb]); % estimate of error

if fun(lb)*fun(ub)>0
    exitFlag = -1;
    error('invalid bracket: exitFlag = %g\n',exitFlag);

elseif fun(root)+errMax>0 && fun(root)-errMax<0;
    exitFlag = 1;
    err = 100*abs([ub-lb]/[ub+lb]); % estimate of error
    done=true;
        
elseif fun(lb)+errMax>0 && fun(lb)-errMax<0;
    exitFlag = 1;
    err = 100*abs([ub-lb]/[ub+lb]); % estimate of error
    root=lb;
    done=true;
        
elseif fun(ub)+errMax>0 && fun(ub)-errMax<0;
    exitFlag = 1;
    err = 100*abs([ub-lb]/[ub+lb]); % estimate of error
    root=ub;
    done=true;

elseif fun(lb)==NaN || Inf || -Inf || isempty(lb);
    exitFlag = -2;
    error('invalid lower bound: exitFlag = %g\n',exitFlag);
    done = true;

elseif fun(ub)==NaN || Inf || -Inf || isempty(ub);
    exitFlag = -2;
    error('invalid upper bound: exitFlag = %g\n',exitFlag);
    done = true;

else
    return;
     
end

%%%%%%%%%%%%%%%%%%%%%%
% MAIN ITERATION LOOP
%%%
while ~done
    
    iter = iter+1;      % increment iteration counter
    
        % determine which way to go with next bracket
    if fun(lb)*fun(root)<0
        ub = root;
    else
        lb = root;
    end
    
    % update approximation of root location based on new bracket
    root = (ub+lb)/2;     
    
    err = 100*abs([ub-lb]/[ub+lb]); % estimate of error
        
    % loop control -- check for termination criteria
    if err<errMax
        exitFlag = 1;
        done = true;
    elseif iter >= iterMax
        exitFlag = 0;
               
        % if the next line executes, it means at least one of the
        % termination criteria was true
        done = true;    
    end

end

Example of function call:
Code:
[root, err, iter, exitFlag] = bisection (@(x)x^3-3*x,-4,2,.0001, 1000)
My problem with the code is that the error flag -2 keeps popping up when I am trying to find a root
 
Physics news on Phys.org
  • #2
Two things: first, Doing fun(lb)==NaN doesn't work. Try it out, NaN==NaN returns 0, not 1. There's a function isnan that you should use instead,
isnan(fun(lb))

Second, you have a serious parenthesis problem. fun(lb)==NaN || Inf doesn't do (fun(lb)==NaN)||(fun(lb)==Inf), it does (fun(lb)==NaN) || Inf and Inf counts as true so your program always takes that part of the if statement and runs it
 
  • #3
Ok, thanks that helps. Between you and a TA I got it to work so thank you!
 

FAQ: How to Implement the Bisection Root-Finding Method in MATLAB?

1. What is a MATLAB Root-Finding Function?

A MATLAB Root-Finding Function is a built-in function in MATLAB that helps find the roots or solutions of a given equation or system of equations. It uses numerical methods to approximate the roots within a specified tolerance.

2. How do I use the MATLAB Root-Finding Function?

To use the MATLAB Root-Finding Function, you need to specify the equation or system of equations, the initial guess for the root(s), and the tolerance value. Then, you can call the function and it will return the approximate root(s) of the equation(s).

3. What types of equations can the MATLAB Root-Finding Function handle?

The MATLAB Root-Finding Function can handle both linear and nonlinear equations. It can also handle systems of equations, where there are multiple equations with multiple variables.

4. How accurate are the results obtained from the MATLAB Root-Finding Function?

The accuracy of the results obtained from the MATLAB Root-Finding Function depends on the specified tolerance value and the complexity of the equation. Generally, the results are accurate up to the specified tolerance, but it is important to note that the function may not always find the exact root(s) due to limitations of numerical methods.

5. Can I use the MATLAB Root-Finding Function for real-world applications?

Yes, the MATLAB Root-Finding Function is commonly used in various fields such as engineering, physics, and economics to solve real-world problems that involve finding solutions to equations or systems of equations.

Back
Top