The secant method is a root-finding algorithm that uses a succession of roots of secant lines to determine the root of a function, typically used when the function is non-linear or when computing the derivative is difficult. Here is an implementation of the secant method algorithm in MATLAB:
main.m961 chars38 lines
To use this function, call it with the desired function f
, initial guesses x0
and x1
, a tolerance tol
, and a maximum number of iterations maxiter
. The function will return the estimated root x
, the value of the function at the root fx
, and the number of iterations used n
. For example, to find the root of the function f(x) = x^3 - 6x^2 + 11x - 6.1
using initial guesses x0 = 0.5
and x1 = 1.5
, with a tolerance of 1e-6
and a maximum of 100
iterations, we would call the function like this:
main.m161 chars4 lines
This will output:
main.m44 chars2 lines
gistlibby LogSnag