KEMBAR78
Numerical Computation Lab Record | PDF
0% found this document useful (0 votes)
12 views2 pages

Numerical Computation Lab Record

The document provides MATLAB code for implementing the Bisection Method to find the root of a function. It includes user inputs for the initial points, tolerance, and maximum iterations, along with error handling for invalid inputs. The code iteratively narrows down the interval until the root is found or the maximum iterations are reached.

Uploaded by

warbeginners.02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Numerical Computation Lab Record

The document provides MATLAB code for implementing the Bisection Method to find the root of a function. It includes user inputs for the initial points, tolerance, and maximum iterations, along with error handling for invalid inputs. The code iteratively narrows down the interval until the root is found or the maximum iterations are reached.

Uploaded by

warbeginners.02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

You might also like