%bisect.m
rt=10.;
fprintf('solve sqrt(%d)=?\n',rt); %solve sqrt(10)
a=round(sqrt(rt))
b=a+1
m=(a+b)/2.
e=10^-4 %hold condition, 精確到小數點第三位,則設到小數點第四位
error=m^2-rt;
count=1;
fprintf('count=%d, error=%f, sqrt(%d)=%f\n',count,error,rt,m);
while abs(error)>e
if error >0
b=m;
else
a=m;
end
a
b
m=(a+b)/2.
error=m^2-rt;
count=count+1;
fprintf('count=%d, error=%f, sqrt(%d)=%f\n',count,error,rt,m);
end