EB 207_Numerical Analysis with MATLAB_Session 1_Bisection Method_2024-2025
EB 207_Numerical Analysis with MATLAB_Session 1_Bisection Method_2024-2025
Objectives:
- Recall constructing M-files to write programs with different commands such as symbolic
manipulation.
- Applying fundamental concepts of presented numerical methods.
- Use MATLAB to implement the bisection method and relate it to the theoretical problems.
This numerical method can be applied for solving nonlinear equations with single variable
like f(x) = 0. It is based on the fact that a root of an equation is the point where its curve
crosses the x-axis (i.e. y = 0). This should be over an interval [a, b] where f (x) is continuous
to let the solution exists and, most importantly, f (a) and f (b) must have the opposite signs.
The procedure to apply this method toward the solution of the nonlinear equation f (x) = 0 is described
in steps as follows:
➱Step 1: Make sure that f (x) is a continuous function over the interval [a, b] to guarantee that f(x) = 0
have a solution within that initial interval.
➱Step 2: To start set 𝑎1 = a and 𝑏1 = b, then make sure that in the given interval [a, b], 𝑓 (𝑎1), and 𝑓(𝑏1)
satisfies the condition of having opposite signs, in other words 𝑓 (𝑎1) 𝑓(𝑏1) < 0.
𝐚𝟏 +𝐛𝟏
➱Step 3: Let p1 = 𝟐
to bisect the interval [𝑎1 , 𝑏1 ] by finding its midpoint p1. Calculate 𝑓 (p1),
if | f (p1 ) | <= tolerance, then stop the iteration.
➱ Step 4: Verify the sign of 𝑓 (p1). If 𝑓 (p1) 𝑓 (𝑎1) < 0, then let b2 ← p1 & a2 ← a1 ; otherwise,
let a2 ← p1 & b2 ← b1 , then go back to step 3 and iteration continues till the desired root is allocated
within the allowable accuracy as |f (pi ) | <= tolerance and so pi is the approximated root.
Page 1 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
MATLAB M-file
% Input function and variables
syms x
f=input('Enter the function, f(x):');
func=inline(f); %to define functions which will be evaluated
a= input('Enter x-lower of the interval, a:');
b= input('Enter x-upper of the interval, b:');
% Graph of the function section
xval = linspace(a,b,100); %to generates n+1 equispaced pts. between [a,b]
for i=1:100
yval(i) = func(xval(i));
end
plot(xval,yval);
grid on;
%%%% Bisection Method section
% condition check
fa = func(a);
fb = func(b);
if fa*fb > 0
error('The interval is incorrect!...Root finding failed');
end
% iterations begin here to find the root
Tol= input('Enter the tolerance, Tol:');
maxItr= input('Enter max. no. of iterations, MaxItr:');
for i=1:maxItr
p=(a+b)/2;
fp =func(p);
if abs(fp) <= Tol ,break;
elseif fp*fa < 0 % root is between a and p
b = p;
else % root is between p and b
a = p;
end
end
r_final= p;
disp('The required root of the equation is:')
disp(r_final)
fprintf('This solution is in %d iterations\n',i)
Page 2 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
Example (1):
Write an M-file that performs different iterations using the bisection method to find an approximate
value to the root of the following equations to achieve an accuracy of 10-4 within the indicated intervals;
Solution:
As the previous M-file is suitable for any functions within any interval and with any tolerance as well
any number of iterations, so all you need now is to run it with the different cases in example 1.
a)
MATLAB command window/ Figure
>> Bisection_method_algorithm
Enter the function, f(x):x^4+3*x^3+2*x+1
Enter x-lower of the interval, a:-2
Enter x-upper of the interval, b:2
Page 3 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
>> Bisection_method_algorithm
Page 4 of 5
Pharos University in Alexandria
Faculty of Engineering 2024-2025
Department of Basic Sciences
b)
Page 5 of 5