Secant Method MATLAB Code
Secant Method MATLAB Code
m 1 of 1
function x = Secant_SSJ(x01,x02,iter_max,error_tol)
% Solve fx = 0 system using Secant method
% Created by Sujit Jogwar on July 31, 2017
% inputs
% x01: initial guess 1
% x02: initial guess 2
% iter_max: maximum allowed iterations
% error_tol: tolerance value for error
% user enters the function f(x) in fun_value.m file
for i = 1:iter_max
f1 = fun_value(x01);
f2 = fun_value(x02);
slope = (f2-f1)/(x02-x01);
x = x02 - f2/slope;
% check convergence
if norm(fun_value(x)) < error_tol
disp(['Convergence achieved in ' num2str(i) ' iterations']);
break; % stops iterations
else
% check if max iterations have reached
if i == iter_max
disp('Maximum iterations reached without satisfying convergence
criteria');
else
% proceed to the next iteration with updated initial
% condition
x01 = x02;
x02 = x;
end
end
end