function y = dichotomy(fun,a,b,tol)
if nargin < 4
tol = 1.0e-5;
end
n = 1;
if feval(fun,a)*feval(fun,b)<0
c = (a+b)/2;
while (abs(b-c)>tol) && (abs(feval(fun,c))>tol)
if (feval(fun,c)*feval(fun,a)>0)
a = c;
c = (a+b)/2;
elseif (feval(fun,c)*feval(fun,a)<0)
b = c;
c = (a+b)/2;
else
y = c;
tol = 100;
end
n = n + 1;
end
y = c;
elseif feval(fun,a)==0
y = a;
elseif feval(fun,b)==0
y = b;
else
disp('there may not be a root in the interval');
end
n