Bisection Method Matlab


What is Bisection Method: The bisection method is also called a binary-search method normally used to find the root of an equation by repeatedly find dividing the equation's interval. 

Application of Bisection Method:

  1. This process is used to locate periodic orbits in a molecular system.
  2. To solving a non-linear equation.
  3. Root finding method of an equation.
  4. To construct a diagram for the bend mode family.

Bisection Method Matlab Code:

-------------start---------------


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 % bcz xr(1)=0 will be zero; i.e: start from 2

xr=(xu+xl)/2;

    if f(xl)*f(xr)>0

        xl=xr;

        xu=xu;

    end

 

    if f(xl)*f(xr)<0

        xu=xr;

        xl=xl;

    end

    if f(xl)*f(xr)==0

        break

    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), '']

fprintf('Values are: %.15f, ', xnew)



-------------end---------------


Solution of an example:


Example of Bisection method



-------------start---------------


f=@(x) x^3-0.165*x^2+3.993*10^-4 ;

xl=input('Enter the 1st value :') ;

xu=input('Enter the last value :');

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 :\n') ;

    xu=input('Enter the end value :\n');

end

 

for i=2:1000

xr=(xu+xl)/2;

    if f(xl)*f(xr)>0

        xl=xr;

        xu=xu;

    end

 

    if f(xl)*f(xr)<0

        xu=xr;

        xl=xl;

    end

    if f(xl)*f(xr)==0

        break

    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), '']

fprintf('Values are: %.15f, ', xnew)

plot(xnew)



-------------end---------------



Post a Comment

Previous Post Next Post