Professional Documents
Culture Documents
f = @(x) exp(-x) - x;
df = @(x) -exp(-x) - 1;
% Define the interval [a, b] and initial guesses for Newton-Raphson and secant methods
a = 0;
b = 1;
x0 = 0.5;
x1 = 1;
% Initialize arrays to store iteration number and true percent relative errors
iterations = 1:max_iter;
errors_bisection = zeros(1, max_iter);
errors_false_position = zeros(1, max_iter);
errors_newton_raphson = zeros(1, max_iter);
errors_secant = zeros(1, max_iter);
% Bisection method
root_bisection = bisection(f, a, b, tol, max_iter);
errors_bisection(1) = abs((root_bisection - true_root) / true_root) * 100;
for i = 2:max_iter
root_bisection = bisection(f, a, b, tol, i);
errors_bisection(i) = abs((root_bisection - true_root) / true_root) * 100;
end
% Newton-Raphson method
root_newton_raphson = newton_raphson(f, df, x0, tol, max_iter);
errors_newton_raphson(1) = abs((root_newton_raphson - true_root) / true_root) * 100;
for i = 2:max_iter
root_newton_raphson = newton_raphson(f, df, x0, tol, i);
errors_newton_raphson(i) = abs((root_newton_raphson - true_root) / true_root) * 100;
end
% Secant method
root_secant = secant(f, x0, x1, tol, max_iter);
errors_secant(1) = abs((root_secant - true_root) / true_root) * 100;
for i = 2:max_iter
root_secant = secant(f, x0, x1, tol, i);
errors_secant(i) = abs((root_secant - true_root) / true_root) * 100;
end