It is a closed method, which is close to the root linearly by removing the average between the two limits of the range, reaching a shorter interval time until close enough to the root.
PSEUDOCODE
Input a, b, f, tol
e=tol+1
po=b
while e>tol
p=(a+b)/2
if f(a)*f(p)<0
b=p
elseif f(b)*f(p)<0
a=p
else
hay raíz
end
e=abs(p-po)
po=p
end
CODE
clc;clear
a=input('ingrese valor inicial: ');
b=input('ingrese valor final: ');
f=input('ingrese la función: ','s');
f=inline(f);
tol=input('ingrese tol: ');
e=tol+1;
po=b;
i=0;
while e>tol
i=i+1;
p=(a+b)/2;
if f(a)*f(p)<0
b=p;
elseif f(b)*f(p)<0
a=p;
else
disp('hay raíz en: ')
disp(p)
end
e=abs(p-po);
po=p;
fprintf('iter: %2i\t a= %f\t b= %f\t p= %f\t f(a)= %f\t f(b)= %f\t f(p)= %f\t e= %f\n',i,a,b,p,f(a),f(b),f(p),e)
end
disp(p)