0% found this document useful (0 votes)
30 views2 pages

Bisection Method2

This document describes the bisection method algorithm for finding the root of a function. It initializes the low and high bounds, evaluates the function at each bound, and iteratively calculates the midpoint until the difference between the bounds is less than the tolerance.

Uploaded by

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

Bisection Method2

This document describes the bisection method algorithm for finding the root of a function. It initializes the low and high bounds, evaluates the function at each bound, and iteratively calculates the midpoint until the difference between the bounds is less than the tolerance.

Uploaded by

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

function m = bisection(f, low, high, tol)

disp('Bisection Method');

% Evaluate both ends of the interval

y1 = feval(f, low);

y2 = feval(f, high);

i = 0;

% Display error and finish if signs are not different

if y1 * y2 > 0

disp('Have not found a change in sign. Will not continue...');

m = 'Error'

return

end

% Work with the limits modifying them until you find

% a function close enough to zero.

disp('Iter low high x0');

while (abs(high - low) >= tol)

i = i + 1;

% Find a new value to be tested as a root

m = (high + low)/2;

y3 = feval(f, m);
if y3 == 0

fprintf('Root at x = %f \n\n', m);

return

end

fprintf('%2i \t %f \t %f \t %f \n', i-1, low, high, m);

% Update the limits

if y1 * y3 > 0

low = m;

y1 = y3;

else

high = m;

end

end

% Show the last approximation considering the tolerance

w = feval(f, m);

fprintf('\n x = %f produces f(x) = %f \n %i iterations\n', m, y3, i-1);

fprintf(' Approximation with tolerance = %f \n', tol);

You might also like