You are on page 1of 10

LAB#08:

Implementation of code of Bisection method and Regula-Falsi


method for solution of Transcendental equations in MATLAB
The learning objectives are:
 How does Root Bracketing Methods work?
 Background of Bracketing Methods.
 Algorithm.
 Convergence sheet.

1. BI-SECTION METHOD:
ALGORITHM:
Newton’s method is a popular technique for the solution of nonlinear equations, but alternative methods
exist which may be preferable in certain situations. The Bisection method is yet another technique for
finding a solution to the nonlinear equation f(x) = 0, which can be used provided that the function f is
continuous. The motivation for this technique is drawn from Bolzano’s theorem for continuous functions.

Theorem (Bolzano):
If the function f(x) is continuous in [a, b] and f(a).f(b) < 0 (i.e. the function f has values with different
signs at a and b), then a value c belongs to (a, b) exists such that f(c) = 0.

The bisection algorithm attempts to locate the value c where the plot of f crosses over zero, by checking
whether it belongs to either of the two sub-intervals [a, xm], [xm, b], where xm is the midpoint xm = (a +
b)/2
The steps to apply the bisection method to find the root of the equation f (x )=0 are
1. Choose
x ℓ and x u as two guesses for the root such that f (x ℓ )f ( x u )< 0 , or in other
x
words, f (x ) changes sign between ℓ and u .
x
2. Estimate the root,
x m , of the equation f (x )=0 as the mid-point between x ℓ and x u
as
x ℓ +x u
xm =
2

3. Now check the following


a) If f (x ℓ )f ( x m )<0 , then the root lies between xℓ and
x m ; then x ℓ=x ℓ and
x u=x m .
b) If f (x ℓ )f ( x m )>0 , then the root lies between x m and x u ; then x ℓ=x m and
x u=x u
x
c) If f (x ℓ )f ( x m )=0 ; then the root is m . Stop the algorithm if this is true.
4. Find the new estimate of the root
x ℓ +x u
xm =
2

Find the absolute relative approximate error as

new old
xm - xm
|∈a| = | |× 100
x mnew
where

x new
m = estimated root from present iteration

x old
m = estimated root from previous iteration

5. Compare the absolute relative approximate error


|∈a| with the pre-specified relative error

tolerance s . If a |∈ |>∈
s , then go to Step 3, else stop the algorithm. Note one should also
check whether the number of iterations is more than the maximum number of iterations allowed.
If so, one needs to terminate the algorithm and notify the user about it.
Procedure:

 Open matlab in Computer


 Press ctrl +N to open editor window
 Write desired code for the given tasks in editor window
 Use for loop, if, if else, nested if, and while loop to get required result
 Press Ctrl+S to save the code
 Press ‘Run’ to execute the program
 Observe the output as shown in the figures given above:
 Output as shown in below figures
 Task Code

MATLAB CODE:

TASK:

Find a root of an equation 2 x3 −2 x−5

find the root using Bisection method in MATLAB.

PROGRAM:
clc
close all
clear all

f=@(x) 2*x.^3-2*x-5; % Let steady-state concentration = x


f(0)
f(5)
f(10)
fplot(f,[0,10])
xlabel('Steady-state concentration, c');
ylabel('f(c)');
title('BI-SECTION METHOD');
gtext('xr');
grid on

xa=0; % Initial guess


xb=5; % Initial guess

fprintf ('i xa xb xr f(xr) RE\n');

for i=1:20
xr=(xa+xb)/2; % Bisection formula

f_xr=2*xr.^3-2*xr-5
RE=abs((xr-xb)/xr)*100;

fprintf ('%6d %15.5f %15.5f %15.5f %15.5f %18.5f \n' ,


i,xa,xb,xr,f_xr,RE)

if f(xr)*f(xa)<0 % Root Bracketing


xb=xr;
xa=xa;
else
xa=xr;
xb=xb;

end %End of if-else Statement


end %End of For-Loop Statement
fprintf('\n The root of given equation = %f \n' , xr); % to print statement

Output:
ans =

-5

ans =

235

ans =

1975

i xa xb xr f(xr) RE

1 0.00000 5.00000 2.50000 21.25000 100.00000

2 0.00000 2.50000 1.25000 -3.59375 100.00000

3 1.25000 2.50000 1.87500 4.43359 33.33333

4 1.25000 1.87500 1.56250 -0.49561 20.00000

5 1.56250 1.87500 1.71875 1.71722 9.09091

6 1.56250 1.71875 1.64063 0.55073 4.76190

7 1.56250 1.64063 1.60156 0.01290 2.43902

8 1.56250 1.60156 1.58203 -0.24497 1.23457

9 1.58203 1.60156 1.59180 -0.11695 0.61350

10 1.59180 1.60156 1.59668 -0.05225 0.30581

11 1.59668 1.60156 1.59912 -0.01973 0.15267

12 1.59912 1.60156 1.60034 -0.00343 0.07628

13 1.60034 1.60156 1.60095 0.00473 0.03812

14 1.60034 1.60095 1.60065 0.00065 0.01907


15 1.60034 1.60065 1.60049 -0.00139 0.00953

16 1.60049 1.60065 1.60057 -0.00037 0.00477

17 1.60057 1.60065 1.60061 0.00014 0.00238

18 1.60057 1.60061 1.60059 -0.00012 0.00119

19 1.60059 1.60061 1.60060 0.00001 0.00060

20 1.60059 1.60060 1.60059 -0.00005 0.00030

The root of given equation = 1.600595

Figure:8.1 Graph to Represent the Roots by Using Bisection Method

2. REGULA-FALSI METHOD:

ALGORITHM:
The steps to apply the false-position method to find the root of the equation f ( x )=0 are as
follows.
x
Choose x L and U as two guesses for the root such that
f ( x L ) f ( x U ) <0
, or in other
words, f ( x )
x
changes sign between x L and U .

Estimate the root, x r of the equation f ( x )=0 as

x U f ( x L )−x L f ( x U )
xr=
f ( x L ) −f ( xU )

Now check the following

If
f ( x L ) f ( x r ) <0
, then the root lies between x L and x r ; then x L =x L and x U =x r .

If
f ( x L ) f ( x r ) >0
, then the root lies between x r and
x U ; then x L =xr and x U =x U .

, then the root is x r . Stop the algorithm.


f ( x L ) f ( x r )=0
If

Find the new estimate of the root

x U f ( x L )−x L f ( x U )
xr=
f ( x L ) −f ( xU )

Find the absolute relative approximate error as


new old
x r −x r
|∈a|=| |×100
x new
r

where

x new
r = estimated root from present iteration

x old
r = estimated root from previous iteration

Compare the absolute relative approximate error


|∈a| with the pre-specified relative error

tolerance
∈s . If |∈a|>∈s , then go to step 3, else stop the algorithm. Note one should also
check

whether the number of iterations is more than the maximum number of iterations allowed. If so,
one needs to terminate the algorithm and notify the user about it.
Note that the false-position and bisection algorithms are quite similar. The only difference is the
formula used to calculate the new estimate of the root x r as shown in steps #2 and #4!

MATLAB CODE:

TASK:

Find a root of an equation 2 x3 −2 x−5

find the root using Regula-Falsi method in MATLAB.

PROGRAM:
clc
close all
clear all

f=@(x) 2*x.^3-2*x-5 % Let steady-state concentration = x


f(0)
f(5)
f(10)
fplot(f,[0,10])
xlabel ('Steady-state concentration, c');
ylabel ('f(c)');
title ('REGULA-FALSI METHOD');
grid on
gtext('xr');

xa=0; % Initial guess


xb=10; % Initial guess

fprintf ('i xa xb xr f(xr) RE\n');

for i=1:15
xr= ((xa*f(xb)-xb*f(xa))./(f(xb)-f(xa))); % Regula-Falsi formula
f_xr=2*xr.^3-2*xr-5;
RE=abs((xr-xb)/xr)*100;

fprintf ('%6d %15.5f %15.5f %15.5f %15.5f %18.5f \n' ,


i,xa,xb,xr,f_xr,RE);

if f(xr)*f(xa)<0 % Root Bracketing


xb=xr;
xa=xa;

else
xa=xr;
xb=xb;

end %End of if-else Statement


end %End of For-Loop Statement
fprintf('\n The root of given equation = %f \n' , xr); % to print statement
Output:

f=

function_handle with value:

@(x)2*x.^3-2*x-5

ans =

-5

ans =

235

ans =

1975

i xa xb xr f(xr) RE
1 0.00000 10.00000 0.02525 -5.05047 39500.00000
2 0.02525 10.00000 0.05069 -5.10113 19625.84913
3 0.05069 10.00000 0.07633 -5.15176 13001.64975
4 0.07633 10.00000 0.10214 -5.20216 9690.03320
5 0.10214 10.00000 0.12815 -5.25209 7703.52597
6 0.12815 10.00000 0.15433 -5.30131 6379.63705
7 0.15433 10.00000 0.18069 -5.34958 5434.44179
8 0.18069 10.00000 0.20721 -5.39663 4725.97789
9 0.20721 10.00000 0.23390 -5.44220 4175.37741
10 0.23390 10.00000 0.26073 -5.48602 3735.31950
11 0.26073 10.00000 0.28771 -5.52779 3375.69079
12 0.28771 10.00000 0.31482 -5.56724 3076.41557
13 0.31482 10.00000 0.34204 -5.60405 2823.59510
14 0.34204 10.00000 0.36937 -5.63795 2607.30147
15 0.36937 10.00000 0.39679 -5.66863 2420.25399

The root of given equation = 0.396785


Figure:8.2 Graph to Represent the Roots by Using Regular-Falsi Method

Comments:

The bisection method is an iterative algorithm used to find roots of continuous functions. The


main advantages to the method are the fact that it is guaranteed to converge if the initial interval
is chosen appropriately, and that it is relatively simple to implement.

The bisection method is used to find the roots of a polynomial equation. It separates the interval
and subdivides the interval in which the root of the equation lies. The principle behind
this method is the intermediate theorem for continuous functions.

The Regula–Falsi Method is a numerical method for estimating the roots of a polynomial f(x). A


value x replaces the midpoint in the Bisection Method and serves as the new approximation of a
root of f(x). The objective is to make convergence faster.

Conclusion:

In this lab I concluded that Regular-Falsi Method find outs the Roots of any equation in a few
iteration as compare to Bisection Method.
Bisection method is the safest and it always converges. The bisection method is the simplest of
all other methods and is guaranteed to converge for a continuous function. It is always possible
to find the number of steps required for a given accuracy.

The Regula–Falsi Method is a numerical method for estimating the roots of a polynomial f(x). A


value x replaces the midpoint in the Bisection Method and serves as the new approximation of a
root of f(x). The objective is to make convergence faster.

You might also like