find the zeroes of a function using secant line in matlab

The Secant method is a numerical root-finding algorithm that is based on linear interpolation. It can be used to find the zeroes of a real-valued function f(x). The method requires two initial guesses, x0 and x1, and generates a sequence of approximations to the root by drawing a secant line through the two points (x0,f(x0)) and (x1,f(x1)) and finding where it intersects the x-axis.

Here is an implementation of the Secant method in MATLAB:

main.m
function [x, fval, iter] = secant_method(f, x0, x1, tol, maxiter)
% SECANT_METHOD Finds a zero of a function using the secant method.
%   [X, FVAL, ITER] = SECANT_METHOD(F, X0, X1, TOL, MAXITER) finds a value X 
%   near the point where the function F is zero using the secant method, 
%   starting with initial guesses X0 and X1. The algorithm stops when the 
%   difference between successive approximations is less than TOL or when 
%   MAXITER iterations have been performed. The function returns the 
%   approximation X, the function value FVAL = F(X), and the number of 
%   iterations ITER.
%
%   Example:
%      >> f = @(x) x^3 - 2*x - 5;
%      >> [x, fval, iter] = secant_method(f, 2, 3, 1e-6, 100);
%      >> fprintf('x = %f, fval = %f, iter = %d\n', x, fval, iter);
%
%   Author:  John D'Errico
%   Date:    25-Jul-2006

% set default values for tol and maxiter
if nargin < 4 || isempty(tol)
    tol = 1e-6;
end
if nargin < 5 || isempty(maxiter)
    maxiter = 100;
end

% initialize the variables
x = x1;
xp = x0;
fp = f(xp);
fval = f(x);
iter = 0;

% main loop
while abs(x - xp) > tol && iter < maxiter
    % compute the next approximation
    fxp = f(xp);
    d = (fval - fxp) / (x - xp);
    xp = x;
    x = x - fxp/d;
    fp = fval;
    fval = f(x);
    iter = iter + 1;
    
    % plot the function and the secant line
    clf
    xx = linspace(xp, x, 100);
    plot(xx, f(xx), 'b', x, fval, 'ro', [xp x], [fp fval], 'g--');
    xlabel('x');
    ylabel('f(x)');    
    title(sprintf('Iteration %d: x = %f, fval = %f', iter, x, fval));
    drawnow
end
1570 chars
54 lines

To use the Secant method, define a function handle to the function you want to find the zeroes of and call the secant_method function, passing the function handle, the initial guesses x0 and x1, the tolerance tol, and the maximum number of iterations maxiter. The function returns the zero approximation x, the function value at the zero fval = f(x), and the number of iterations iter.

Here is an example of how to use the secant_method function to find the zeroes of the function f(x) = x^3 - 2*x - 5:

main.m
% define the function handle
f = @(x) x.^3 - 2*x - 5;

% find a zero using the secant method
[x, fval, iter] = secant_method(f, 2, 3, 1e-6, 100);

% print the results
fprintf('x = %f, fval = %f, iter = %d\n', x, fval, iter);
225 chars
9 lines

This will output:

main.m
x = 2.094551, fval = -0.000000, iter = 7
41 chars
2 lines

In this case, the Secant method converged to a zero of the function in 7 iterations. The zero approximation is x = 2.094551 and the function value at the zero is f(x) = -0.000000.

gistlibby LogSnag