Saturday, March 18, 2023

Bisection method: root finding for nonlinear equation Numerically using MATLAB

 The bisection method is a simple iterative algorithm that works by repeatedly dividing an interval in half and selecting the subinterval in which the root must lie. Here's how the algorithm works:

  1. Choose an initial interval [a, b] that brackets the root of the equation f(x) = 0, i.e., f(a) and f(b) have opposite signs.

  2. Divide the interval [a, b] in half to get the midpoint c = (a + b) / 2.

  3. Evaluate the function at the midpoint f(c).

  4. If f(c) = 0, then c is the root of the equation.

  5. If f(a) * f(c) < 0, then the root lies in the left subinterval [a, c]. Set b = c and go back to step 2.

  6. If f(b) * f(c) < 0, then the root lies in the right subinterval [c, b]. Set a = c and go back to step 2.

  7. Repeat steps 2-6 until the desired accuracy is achieved.


Here is the Matlab code for solving the nonlinear equations using Matlab. The first code and result show when the iteration is controlled by the number of iterations. whereas the second is based on the minimum error. When the minimum error in the computation reaches, iteration will be stopped.

A. Stopping criteria: Maximum iteration.
=======================================================================

% Application of Bisection in Root finding
clear all, clc
syms x; % Setting x as symbolic variable
y = input('Enter non-linear equations: ');
a = input('Enter lower limit: ');
b = input('Enter upper limit: ');
iteration = input('maximum Iteration: '); % basic stopping criterion
er=100; % error initialization
%evaluation of f(x) for the boundary conditions
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\tb\t\tc\t\ter\t\tf(c)\n');
fprintf('%f\t%f\t%f\t%f\t%f\n',a,b,c,fc, er);
for i = 1:iteration-1 % since the iteration starts before for loop
if fa*fc< 0
b =c;
c = (a+b)/2;
er=abs((c-b)/c)*100; % new error distribution
else
a =c;
c = (a+b)/2;
er=abs((c-a)/c)*100;
end
fc = eval(subs(y,x,c));
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
fprintf('%f\t%f\t%f\t%f\t%f\n',a,b,c,fc, er);
end
fprintf('\nRoot is: %f\n', c);
end


Here is the result
===========================
Enter non-linear equations:
exp(x)-2
Enter lower limit:
0
Enter upper limit:
1
maximum Iteration:
4


a b c er f(c)
0.000000 1.000000 0.500000 -0.351279 100.000000
0.500000 1.000000 0.750000 0.117000 33.333333
0.500000 0.750000 0.625000 -0.131754 20.000000
0.625000 0.750000 0.687500 -0.011263 9.090909

Root is: 0.687500


B. Stopping criteria: Minimum error. When we want to iterate up to
a certain minimum error. Here is a simple example that is simplified
using Matlab


==========================================================

% basic stopping criterion is the maximum error
clear all, clc
% Application of Bisection in Root finding
syms x; % Setting x as symbolic variable
y = input('Enter non-linear equations: ');
a = input('Enter lower limit: ');
b = input('Enter upper limit: ');
e = input('Tolerable error in decimal: '); % basic stopping criterion
er=1; % error initialization
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\tb\t\tc\t\ter\n');
fprintf('%f\t%f\t%f\t%f\n',a,b,c,er);
while er>e
if fa*fc< 0
b =c;
c = (a+b)/2;
er=abs((c-b)/c); % new error distribution
else
a =c;
c = (a+b)/2;
er=abs((c-a)/c);
end
fc = eval(subs(y,x,c));
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
fprintf('%f\t%f\t%f\t%f\n',a,b,c,er);
end
fprintf('\nRoot is: %f\n', c);
end


Here is the result

=================
Enter non-linear equations:
exp(x)-2
Enter lower limit:
0
Enter upper limit:
1
Tolerable error in decimal:
0.01


a b c er
0.000000 1.000000 0.500000 1.000000
0.500000 1.000000 0.750000 0.333333
0.500000 0.750000 0.625000 0.200000
0.625000 0.750000
0.687500 0.090909
0.687500 0.750000 0.718750 0.043478
0.687500 0.718750 0.703125 0.022222
0.687500 0.703125 0.695312 0.011236
0.687500 0.695312
0.691406 0.005650

Root is: 0.691406
 

No comments:

Post a Comment