You are on page 1of 6

Tutorial 3

“Bisection method”
Instructed by Chhorn sopheaktra

1. Plotting method
There are many ways to find the root of an equation. One of them is
using the plotting tool. This method is very simple because the user can
use matlab to display the graph of any equation and they can spot the
root of the equation manually.

1.1. Algorithm

Example: Find the root of equation x3+2x-1=0 within the interval of [0


3]. You can do it by using plotting method as show in below algorithm:
 Create the script in matlab
 Write the equation (i.e. y=x3+2x-1)
 Create the interval of x with specific step point
(i.e. x=0:0.001:3)
 Using matlab command “plot” to display the graph y is on the y-axis
and x is in the x-axis
 Spot the root manually by using data cursor as show in figure 1
below.
Note: there is an example code in below section or you can use example
code with the file name plotting method.
Data cursor

Figure 1

1.2. Practice 1

Create the script to calculate the root of equation x2+sin(x)=3. The


interval of this calculation is [0 10].

2. Bisection method
The Bisection Method, also called the interval halving method,
the binary search method, or the dichotomy method. is based on the
Bolzano’s theorem for continuous functions.
Theorem (Bolzano): If a function f(x) is continuous on an interval [a,
b] and f(a)·f(b) < 0, then a value c ∈ (a, b) exist for which f(c) = 0.
2.1. Algorithm

 Create script in matlab


 Write the equation (i.e. y= f(x) =x3+2x-1)
 Define interval [a b]. (i.e. [0 6])
 Verify Bolzano condition
 Calculate the middle value of interval xr=(a+b)/2
 Set new interval base on below condition:
 If f(a).f(xr)<0, the new interval is [a xr]
 If f(b).f(xr)<0, the new interval is [xr b]
|xp  xc |
 Calculate the absolute error (error= )
xc
 Calculate new middle value base on the new interval
 Keep the process until the error value is smaller than desire value

Remark: the number of time that use to calculate the middle value is
called “iteration”. There is an example code in below section or you
can use example code with the file name plotting method.

2.2. Practice 2

Create the script to calculate the root of equation x2+sin(x)=3. The


interval of this calculation is [0 10]. The method of calculation is
bisection method.
Example code
% -------------prepare by chhorn sopheaktra-------------%%
% -------------Plotting Method-------------%%
clear all;
clc;
close all;
y=input('Input function f(x) =');%input equation in format of f(x)=@(x)
interval=input('Input interval [a b] =');%input interval
Accuracy=input('Input accuracy A(%)=');%input accuracy
interval_frame=interval(1,1):Accuracy:interval(1,2);%creat interval frame
k=1;%initial number for storing data in array
number_of_array=size(interval_frame);
line_zero=zeros(number_of_array(1,2));
for i=interval(1,1):Accuracy:interval(1,2)
A(k)=y(i);% calculate y=f(x) by changing x
k=k+1;% update array number
end
plot(interval_frame,A);% plot graph of input equation
hold on;
plot(interval_frame,line_zero);
title('plotting method');
xlabel('X');
ylabel('Y=f(x)');
grid minor;
% -------------prepare by chhorn sopheaktra-------------%%
% -------------Bisection Method-------------%%
clear all;
close all;
clc;

y=input('Input function f(x) =');%input equation in format of f(x)=@(x)_ _ _


interval=input('Input interval [a b] =');%input interval
max_iteration=input('Input maximum interation =');% input number of iteration
dp=input('Input minimum error =');%set maximum error
a=interval(1,1);% left side value of interval
b=interval(1,2);% right side value of interval
iteration=1;% initial value of iteration (start from 1);
xi=0;% initial value of root (x)
k=1;% initial value of array pointer
if (y(a)*y(b)<0)
while (1)
N_inter(k)=iteration;% store number of iteration to array name N_inter
x(k)=(a+b)/2;% calculate root of equation at k interation (start from 1st
iteration)
error(k)=abs((x(k)-xi)/xi);% calculate error of the root
if (error(k)<dp)% set condition for root value
break; % exite the loop
elseif (iteration>=max_iteration)% set condition for iteration value
disp('Can not find the root');
break; % exite the loop
end
if (y(a)*y(x(k))<0)
b=x(k); % set new interval value
elseif(y(b)*y(x(k))<0)
a=x(k); % set new interval value
end
xi=x(k); % update the value of the root
iteration=iteration+1; % update the value of iteration
k=k+1; % update the value of array pointer
end
fprintf('%22s %11s %14s\n','Iteration','Result','Error'); %display header
fprintf('%22s %14s %14s\n','---------','----------','----------');
fprintf('%20.4f | %12.6f | %12.6f |\n',[N_inter;x;error]);%display result of
each iteration

else
disp('Incorrect interval, Can not apply bisection method');
end

You might also like