
clc,clear

syms x
f = x^3 -x-1;
a = 1;
b = 2;
e = 1e-4;
n=1;

hold on;
plot(a:0.01:b,subs(f,a:0.01:b),'b','Linewidth',2);

while abs(subs(f,a)-subs(f,b))>e
    
    c = (a+b)/2;
    plot(c,subs(f,c),'ro','Linewidth',2);
    pause(0.5);
    
    if subs(f,a)*subs(f,c)<0
       b = c;
       fprintf('第%d步：有根区间为：[%f,%f]\n',n,a,b);
       n = n+1;
       continue;
    end
    
    a = c;
    fprintf('第%d步：有根区间为：[%f,%f]\n',n,a,b);
    n = n+1;
    
end