1. Engineering
2. Computer Science

###### Question details

function [x,n] = bisection(fun, a, b, maxtol, maxitr)

if nargin<5, maxitr=50; end
if nargin<4, maxtol=1e-4; end
x = a;
k = 0;
funa = fun(a); funb = fun(b); funx = fun(x);
er = abs(funx);

if abs(funa) < eps
fprintf('Left bracket point is the root = %10.4e ',a), return
end
if abs(funb) < eps
fprintf('Right bracket point is the root = %10.4e ',b), return
end

if funa*funb > 0
error(['The function values at the bracket endpoints must differ in sign. ', ...
'This means that there is either no root or possibly an even number ', ...
'of roots in the initial bracket.'])
end

fprintf(' Iter# a f(a) b f(b) x f(x) ');
fprintf('%i %f %f %f %f %f ',k,a,fun(a),b,fun(b))

%% Execute the bisection algorithm
while er >= maxtol && k < maxitr
k = k + 1;
x = (a + b)/2;
funx = fun(x);
if abs(funx) < eps
fprintf(' In this iteration, the midpoint is found to be a root = %10.4e ',x), break
end

if fun(a)*funx < 0, b = x;
else a = x;
end
er = abs(funx);
fprintf(' %i %f %f %f %f %f %f %f ',k,a,fun(a),b,fun(b),x,funx)
end
fprintf(' ')
n = k;

%% Check results for no convergence
if n == maxitr && er >= maxtol
fprintf(' ')
warning('Maximum number of iterations reached before convergence')
fprintf(' Solution not obtained in %d iterations. ',maxitr);
end

end

falsepos.

function [root, err, numIter, exitFlag]=falsepos(func,lb,ub,err_max,iter_max)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This functions find the the root of a function by repeatedly bisecting an interval and then selecting
% a subinterval in which a root must lie for further processing.

% function [root,err,numIter,exitflag] = falsepos(fun,lb,ub,err_max,iter_max)
% INPUT:
% func: an input function
% lb,ub: interval limits
% err_max: maximum interval size in which final root should lie
% iter_max: maximum number of iterations allowed

% OUTPUT:
% root : the final root of the function func
% err: final interval size of the interval in which root lies
% numIter: number of iterations it took to obtain the root
% exitFlag: status if return (>1 for normal return and -1 if error)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT TESTING AND DEFAULT VALUES

root=[]; func_val=[]; err=[]; numIter=[]; % Output set to empty matricesinitially
exitFlag=1;

if(nargin)==3 % check if last two arguments have been passed or not
err_max=0.0001; % assign the default values
iter_max=50;
elseif (nargin)==4 % check if last argument have been passed or not
iter_max=50; % assign the default values
elseif (nargin)<3 || nargin>5 % if less than three or more than five arguments have been passed, raise an error and return
warning('Insufficient arguments passed!');
return;

end

if func(lb)*func(ub)>0 % check if the function value at initial points are not same sign, raise and error return
disp('Probelm with interval signs. Try again!');
exitFlag=-1;
return;
elseif (lb>ub) % check if x_min<x_max i.e. intervals ae not in same order,raise and error return
disp('Probelm with interval size. Try again!');
exitFlag=-1;
return;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAIN BODY OF FUNCTION:

n=0; % number of iteartions computed yet
func_x_max=func(ub); func_x_min=func(lb);

c=lb-((ub-lb)/(func_x_max-func_x_min))*func_x_min; % find the next guess point
err = abs(ub-lb); % take initial error estimate

% Uncomment the following line and line 72 to see result in each iteration
% fprintf('Iteration x_min x_max Root Error estimated ');
while err > err_max && n<iter_max % loop will continue as long as this criteria holds true
n=n+1; % iteration number
if func(lb)*func(c)<0 % select the interval of root
ub = c;

else % select the interval of root
lb = c;
end
func_x_max=func(ub); func_x_min=func(lb);

c = lb-((ub-lb)/(func_x_max-func_x_min))*func_x_min; % find the next guess point
err = abs(ub-lb);

% Uncomment the following line to see result in each iteration
% fprintf('%d %3.6f %3.4f %3.4f %3.8f ',n,lb,ub,c,err); % display the iteration result
end
root=c;
func_val=func(c);
err=err;
numIter=n;
end