Bisection Method in MATLAB

6 Min Read

Bisection method is a popular root finding method of mathematics and numerical methods. This method is applicable to find the root of any polynomial equation f(x) = 0, provided that the roots lie within the interval [a, b] and f(x) is continuous in the interval.

This method is closed bracket type, requiring two initial guesses. The convergence is linear and it gives good accuracy overall. Compared to other rooting finding methods, bisection method is considered to be relatively slow because of its slow and steady rate of convergence.

Earlier we discussed a C program and algorithm/flowchart of bisection method. Here, we’re going to write a source code for Bisection method in MATLAB, with program output and a numerical example.

Bisection Method Theory:

Bisection method is based on Intermediate Value Theorem. According to the theorem: “If there exists a continuous function f(x) in the interval [a, b] and c is any number between f(a) and f(b), then there exists at least one number x in that interval such that f(x) = c.”

The intermediate value theorem can be presented graphically as follows:


Here’s how the iteration procedure is carried out in bisection method (and the MATLAB program):

The first step in iteration is to calculate the mid-point of the interval [ a, b ]. If c be the mid-point of the interval, it can be defined as:

c = ( a+b)/2

The function is evaluated at ‘c’, which means f(c) is calculated. Now, three cases may arise:

  1. f(c) = 0 : c is the required root of the equation.
  2. f(b) * f(c) > 0 : if the product of f(b) and f(c) is positive, the root lies in the interval [a, c].
  3. f(b) * f(c) < 0 : if the product of f(b) and f(c) is negative, the root lies in the interval [ b, c].

In the second iteration, the intermediate value theorem is applied either in [a, c] or [ b, c], depending on the location of roots. And then, the iteration process is repeated by updating new values of a and b. The program for bisection method in MATLAB works in similar manner.

Bisection Method in MATLAB Code:

% Bisection Method in MATLAB
a=input('Enter function with right hand side zero:','s');
f=inline(a);

xl=input('Enter the first value of guess interval:') ;
xu=input('Enter the end value of guess interval:');
tol=input('Enter the allowed error:');

if f(xu)*f(xl)<0
else
    fprintf('The guess is incorrect! Enter new guesses\n');
    xl=input('Enter the first value of guess interval:\n') ;
    xu=input('Enter the end value of guess interval:\n');
end

for i=2:1000
xr=(xu+xl)/2;
if f(xu)*f(xr)<0
    xl=xr;
else
    xu=xr;
end

if f(xl)*f(xr)<0
    xu=xr;
else
    xl=xr;
end

xnew(1)=0;
xnew(i)=xr;
if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end
end
str = ['The required root of the equation is: ', num2str(xr), '']

In this code for bisection method in Matlab, first the equation to be solved is defined, and it is then assigned with a variable f using inline() command. The program then asks for the values of guess intervals and allowable error.

The iteration process is similar to that described in the theory above. Or, you can go through this algorithm to see how the iteration is done in bisection method. Iteration continues till the desired root is allocated within the allowable error.

Here’s a sample output of this MATLAB program:

Bisection Method Example:

Now, lets analyze the above program of bisection method in Matlab mathematically. We have to find the root of x2 -3 = 0, starting with the interval [1, 2] and tolerable error 0.01.

Given that, f(x) = x2 -3 and a =1 & b =2
Mid-value of the interval, c = (a+b)/2 = (a+2)/2 = 1.5
f(c) = 1.52 -3 = -0.75

Check for the following cases:

  1. f(c) ≠ 0 : c is not the root of given equation.
  2. f(c ) * f(a) = -0.75 * -2 = 1.5 > 0 : root doesn’t lie in [1, 1.5]
  3. f(c ) * f( b) = -0.75 * 1= -0.75 < 0 : root lies in [1.5, 2]

The process is then repeated for the new interval [1.5, 2]. The table shows the entire iteration procedure of bisection method and its MATLAB program:

Thus, the root of x2 -3 = 0 is 1.7321. It is slightly different from the one obtained using MATLAB program. But, this root can be further refined by changing the tolerable error and hence the number of iteration.

If you have any questions regarding bisection method or its MATLAB code, bring them up from the comments. You can find more Numerical methods tutorial using MATLAB here.

Share This Article
8 Comments
  • Hi

    Thanks for the great explanation. I just cannot figure out the exact role of xnew! I searched it on Matlab’s help, but it was not helpful.

    Thx

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version