Bisection method add iteration table into my code

12 views (last 30 days)
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...');
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);
If it is important my code works by entering variables into command line like:
my_fun = @(x) exp(x) - 3*x;
low = 0;
high = 1;
tolerance = .00001;
x = bisection(my_fun, low, high, tolerance);
I want to ask how could I add an iterations graphic into my code? I want to see a x value at each iteration.

Answers (1)

Alan Stevens
Alan Stevens on 27 Dec 2022
Something like this?
my_fun = @(x) exp(x) - 3*x;
low = 0;
high = 1;
tolerance = .00001;
[x, x0] = bisection(my_fun, low, high, tolerance); %%%%%%%%%%%%%%%%%
Bisection Method Iter low high x0 0 0.000000 1.000000 0.500000 1 0.500000 1.000000 0.750000 2 0.500000 0.750000 0.625000 3 0.500000 0.625000 0.562500 4 0.562500 0.625000 0.593750 5 0.593750 0.625000 0.609375 6 0.609375 0.625000 0.617188 7 0.617188 0.625000 0.621094 8 0.617188 0.621094 0.619141 9 0.617188 0.619141 0.618164 10 0.618164 0.619141 0.618652 11 0.618652 0.619141 0.618896 12 0.618896 0.619141 0.619019 13 0.619019 0.619141 0.619080 14 0.619019 0.619080 0.619049 15 0.619049 0.619080 0.619064 16 0.619049 0.619064 0.619057 x = 0.619057 produces f(x) = 0.000005 16 iterations Approximation with tolerance = 0.000010
plot(0:numel(x0)-1,x0,'*--'),grid %%%%%%%%%%%%%%%%%%%%%%%%
function [m, x0] = 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...');
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);
x0(i) = 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);
end

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!