0% found this document useful (0 votes)
5 views2 pages

Secant

The document outlines a MATLAB implementation of the Secant Method to solve the equation e^(-x) - x = 0. It initializes values, iterates to find the root, and calculates both relative and true errors while checking for convergence. The process continues until either convergence is achieved or the maximum number of iterations is reached.

Uploaded by

wonwoong5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Secant

The document outlines a MATLAB implementation of the Secant Method to solve the equation e^(-x) - x = 0. It initializes values, iterates to find the root, and calculates both relative and true errors while checking for convergence. The process continues until either convergence is achieved or the maximum number of iterations is reached.

Uploaded by

wonwoong5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

[Secant]

% Secant Method to solve e^(-x) - x = 0


clear all;

% Define the function


f = @(x) exp(-x) - x; % Original equation

% Initial guesses
x_prev = 0; % x_{-1}
x_curr = 1; % x_0

tol = 1e-6; % Tolerance for convergence


max_iter = 100; % Maximum number of iterations

% True solution for error calculation (approximately)


true_solution = 0.567143290409784; % Known solution of e^(-x) = x

% Header for output


fprintf('Iter\t x_n\t\t Rel. Error (%%)\t True Error (%%)\n');

for iter = 1:max_iter


% Secant formula: x_next = x_curr - f(x_curr) * (x_curr - x_prev) / (f(x_curr) -
f(x_prev))
x_next = x_curr - f(x_curr) * (x_curr - x_prev) / (f(x_curr) - f(x_prev));

% Percent relative error calculation


if iter == 1
rel_error = NaN; % No relative error for the first iteration
else
rel_error = abs((x_next - x_curr) / x_next) * 100;
end

% Percent true error calculation (if true solution is known)


true_error = abs((true_solution - x_next) / true_solution) * 100;

% Print current iteration results


fprintf('%d\t %.12f\t %.12f\t %.12f\n', iter, x_next, rel_error, true_error);

% Check for convergence


if abs(x_next - x_curr) < tol
fprintf('Converged to solution: x = %.6f after %d iterations.\n', x_next, iter);
break;
end

% Update for next iteration


x_prev = x_curr;
x_curr = x_next;
end

% If maximum iterations are reached without convergence


if iter == max_iter
fprintf('Did not converge within %d iterations.\n', max_iter);
end

You might also like