I have forgotten

COST (GBP)
3.97
0.00
0

# Secant

viewed 3584 times and licensed 95 times
Calculates the zeros of a function using the secant method.
Controller: CodeCogs
Contents

C++

## Secant

 doublesecant( double (*f)(double)[function pointer] double x0 = -1E+7 double x1 = 1E+7 double eps = 1E-10 int maxit = 1000 )
This is a root-finding algorithm which assumes a function to be approximately linear in the region of interest. Each improvement is taken as the point where the approximating line crosses the axis. The secant method retains only the most recent estimate, so the root does not necessarily remain bracketed.

When the algorithm does converge, its order of convergence is
$\lim_{k&space;\rightarrow&space;\infty}&space;|\epsilon_{k&space;+&space;1}|&space;\approx&space;C&space;|\epsilon|^{\phi}$
where C is a constant and $\inline&space;\phi$ is the golden ratio.

Let us now derive the actual recurrence relation used with this method.
$f'(x_{n&space;-&space;1})&space;\approx&space;\frac&space;{f(x_{n&space;-&space;1})&space;-&space;f(x_{n&space;-&space;2})}&space;{x_{n&space;-&space;1}&space;-&space;x_{n&space;-&space;2}}$
$f(x_n)&space;\approx&space;f(x_{n&space;-&space;1})&space;+&space;f'(x_{n&space;-&space;1})&space;(x_n&space;-&space;x_{n&space;-&space;1})&space;=&space;0$
$f(x_{n&space;-&space;1})&space;+&space;\frac&space;{f(x_{n&space;-&space;1})&space;-&space;f(x_{n&space;-&space;2})}&space;{x_{n&space;-&space;1}&space;-&space;x_{n&space;-&space;2}}&space;(x_n&space;-&space;x_{n&space;-&space;1})&space;=&space;0$
therefore
$x_n&space;=&space;x_{n&space;-&space;1}&space;-&space;\frac&space;{f(x_{n&space;-&space;1})&space;(x_{n&space;-&space;1}&space;-&space;x_{n&space;-&space;2})}&space;{f(x_{n&space;-&space;1})&space;-&space;f(x_{n&space;-&space;2})}$

To give you a better idea on the way this method works, the following graph shows different iterations in the approximation process. Here is the associated list of pairs chosen at consecutive steps

$(a_0,&space;b_0)&space;\quad&space;(a_1,&space;b_0)&space;\quad&space;(a_2,&space;b_0)$

##### MISSING IMAGE!

This algorithm finds the roots of the user-defined function f starting with an initial interval [x0, x1] and iterating the sequence above until either the accuracy eps is achieved or the maximum number of iterations maxit is exceeded.

## References:

• F.R. Ruckdeschel, "BASIC Scientific Subroutines", Vol. II, BYTE/McGRAWW-HILL, 1981
• MathWorld, http://mathworld.wolfram.com/SecantMethod.html

### Example 1

#include <codecogs/maths/rootfinding/secant.h>

#include <iostream>
#include <iomanip>

// user-defined function
double f(double x) {
return (x - 4) * (x + 5);
}

int main()
{
double x = Maths::RootFinding::secant(f, -2, 10);

std::cout << "The calculated zero is X = " << std::setprecision(14) << x << std::endl;
std::cout << "The associated ordinate value is Y = " << f(x) << std::endl;
return 0;
}
Output:
The calculated zero is X = 3.9999998402552
The associated ordinate value is Y = -1.4377032269309e-006

### Parameters

 f the user-defined function x0 Default value = -1E+7 x1 Default value = 1E+7 eps Default value = 1E-10 maxit Default value = 1000

### Authors

Lucian Bentea (August 2005)
##### Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.