clc; clear; format long;
f = @(x) x^3 - x - 2;
a = 1; b = 2; tol = 1e-6;
for i = 1:100
c = (a+b)/2;
if abs(f(c)) < tol, break; end
if f(a)*f(c) < 0, b = c; else a = c; end
end
fprintf('Root = %.8f after %d iterations\n', c, i);
clc; clear; format long;
f = @(x) cos(x) - x;
a = 0; b = 1; tol = 1e-6;
for i = 1:100
c = (af(b) - bf(a))/(f(b)-f(a));
if abs(f(c)) < tol, break; end
if f(a)*f(c) < 0, b = c; else a = c; end
end
fprintf('Root = %.8f after %d iterations\n', c, i);
clc; clear; format long;
f = @(x) x^3 - x - 2;
a = 1; b = 2; tol = 1e-6;
for i = 1:100
c = (a+b)/2;
if abs(f(c)) < tol, break; end
if f(a)*f(c) < 0, b = c; else a = c; end
end
fprintf('Root = %.8f after %d iterations\n', c, i);
clc; clear; format long;
f = @(x) cos(x) - x;
a = 0; b = 1; tol = 1e-6;
for i = 1:100
c = (af(b) - bf(a))/(f(b)-f(a));
if abs(f(c)) < tol, break; end
if f(a)*f(c) < 0, b = c; else a = c; end
end
fprintf('Root = %.8f after %d iterations\n', c, i);