% 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