M = 2
a = 1
fa=a
b = 2
k = 0;
eval=[];
xval=[];
while b-a > eps
x = (a + b)/2;
xval=[xval x];
eval=[eval abs(x-sqrt(M))];
if x^2 > M
b = x;
else
a = x;
end
k = k + 1
end
pause
semilogy(eval)
hold
pause
M = 2
a = 1
b = 2
k = 0;
xval=[]
fa=a^2-M;
fb=b^2-M;
eval=[];
while b-a > eps
x = a-fa*(b-a)/(fb-fa);
xval=[xval x];
fn=x^2-M;
eval = [eval abs(fn)];
if fn>0
b = x;
fb=fn;
else
a = x;
fa=fn;
end
k = k + 1
end
pause
semilogy(eval)
