Bisection Method in MATLAB

The Bisection Method is a numerical method used to find the root of a function. It is a simple and robust method that works by repeatedly dividing an interval in half and checking which half the root lies in, and then repeating the process on the half-interval that contains the root.

Here are the steps for the Bisection Method:

  • Choose an initial interval [a, b] that contains the root of the function f(x).
  • Compute the midpoint c = (a + b)/2.
  • Evaluate the function at the midpoint f(c).
  • If f(c) = 0, then c is the root and we are done.
  • If f(c) has the same sign as f(a), then the root is in the interval [c, b]. Otherwise, the root is in the interval [a, c].
  • Repeat steps 2-5 until the desired level of accuracy is achieved.

The Bisection Method is guaranteed to converge to a root of the function as long as the function is continuous and changes sign in the interval [a, b]. However, it can be slow to converge, especially for functions that are not well-behaved, oscillatory, or have multiple roots. In practice, other methods such as the Newton-Raphson method or the Secant method may be more efficient for finding roots.

MATLAB Code of Bisection Method

Here is an example MATLAB code for implementing the Bisection Method:

function root = bisection_method(f, a, b, tol, max_iter)
% Inputs:
% f - function handle representing the function to find the root of
% a, b - the interval to search for the root in
% tol - the tolerance for the root
% max_iter - the maximum number of iterations to perform
% Output:
% root - the estimated root of the function

% Check that the function changes sign in the interval
if f(a) * f(b) >= 0
    error('Function must change sign in the interval.')

% Initialize variables
iter = 0;
error = inf;
c = (a + b) / 2;

% Iterate until convergence or maximum number of iterations reached
while error > tol && iter < max_iter
    fc = f(c);
    % Check if c is the root
    if fc == 0
    % Update the interval
    if f(a) * fc < 0
        b = c;
        a = c;
    % Compute the new midpoint and error
    c_new = (a + b) / 2;
    error = abs(c_new - c);
    c = c_new;
    iter = iter + 1;

% Check for convergence
if error > tol
    warning('Bisection method did not converge to desired tolerance.')

% Return the root
root = c;

To use this function, you would define your function ‘f‘ as a MATLAB function handle and call the ‘bisection_method‘ function with the appropriate inputs. For example:

f = @(x) x^3 - 2*x - 5;
a = 1;
b = 3;
tol = 1e-6;
max_iter = 100;

root = bisection_method(f, a, b, tol, max_iter);

This would estimate the root of the function ‘f(x) = x^3 - 2x - 5‘ in the interval [1, 3] with a tolerance of 1e-6 and a maximum of 100 iterations.

Discover more from Reads Blog

Subscribe to get the latest posts to your email.

About The Author

Discover more from Reads Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading