BISECTION METHOD-1
MATLAB CODE
f = @(x) cos(x) - x;
a = input('Enter the first iritation point :');
b = input('Enter the second iritation point :');
tol = input('Enter the tollerance value :');
max_iter = input('Enter the maximum number of iteration :');
root = bisection_method(f, a, b, tol, max_iter);
function root = bisection_method(func, a, b, tol, max_iter)
if func(a) * func(b) >= 0
error('The function must have different signs at a and b');
end
for iter = 1:max_iter
c = (a + b) / 2;
if abs(func(c)) < tol || (b - a) / 2 < tol
root = c;
fprintf('Root found at x = %.15f after %d iterations\n',
root, iter);
return;
end
if func(c) * func(a) < 0
b = c;
else
a = c;
end
end
root = (a + b) / 2;
fprintf('Maximum iterations reached. Approximate root at x =
%.15f\n', root);
end
OUTPUT