Jump to navigationJump to search
The first two iterations of the secant method. The red curve shows the function f, and the blue lines are the secants. For this particular case, the secant method will not converge to the visible root.
In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a functionf. The secant method can be thought of as a finite-difference approximation of Newton's method. However, the method was developed independently of Newton's method and predates it by over 3000 years.[1]
The method[edit]
The secant method is defined by the recurrence relation
As can be seen from the recurrence relation, the secant method requires two initial values, x0 and x1, which should ideally be chosen to lie close to the root.
Derivation of the method[edit]
Starting with initial values x0 and x1, we construct a line through the points (x0, f(x0)) and (x1, f(x1)), as shown in the picture above. In slope–intercept form, the equation of this line is
The root of this linear function, that is the value of x such that y = 0 is
We then use this new value of x as x2 and repeat the process, using x1 and x2 instead of x0 and x1. We continue this process, solving for x3, x4, etc., until we reach a sufficiently high level of precision (a sufficiently small difference between xn and xn−1):
Convergence[edit]
The iterates of the secant method converge to a root of , if the initial values and are sufficiently close to the root. The order of convergence is φ, where
is the golden ratio. In particular, the convergence is superlinear, but not quite quadratic.
This result only holds under some technical conditions, namely that be twice continuously differentiable and the root in question be simple (i.e., with multiplicity 1).
If the initial values are not close enough to the root, then there is no guarantee that the secant method converges. There is no general definition of 'close enough', but the criterion has to do with how 'wiggly' the function is on the interval . For example, if is differentiable on that interval and there is a point where on the interval, then the algorithm may not converge.
Comparison with other root-finding methods[edit]
The secant method does not require that the root remain bracketed, like the bisection method does, and hence it does not always converge. The false position method (or regula falsi) uses the same formula as the secant method. However, it does not apply the formula on and , like the secant method, but on and on the last iterate such that and have a different sign. This means that the false position method always converges.
The recurrence formula of the secant method can be derived from the formula for Newton's method
by using the finite-difference approximation
The secant method can be interpreted as a method in which the derivative is replaced by an approximation and is thus a quasi-Newton method.
If we compare Newton's method with the secant method, we see that Newton's method converges faster (order 2 against φ ≈ 1.6). However, Newton's method requires the evaluation of both and its derivative at every step, while the secant method only requires the evaluation of . Therefore, the secant method may occasionally be faster in practice. For instance, if we assume that evaluating takes as much time as evaluating its derivative and we neglect all other costs, we can do two steps of the secant method (decreasing the logarithm of the error by a factor φ2 ≈ 2.6) for the same cost as one step of Newton's method (decreasing the logarithm of the error by a factor 2), so the secant method is faster. If, however, we consider parallel processing for the evaluation of the derivative, Newton's method proves its worth, being faster in time, though still spending more steps.
Generalizations[edit]
Broyden's method is a generalization of the secant method to more than one dimension.
The following graph shows the function f in red and the last secant line in bold blue. In the graph, the x intercept of the secant line seems to be a good approximation of the root of f.
A computational example[edit]
The secant method is applied to find a root of the function f(x) = x2 − 612. Here is an implementation in the MATLAB language (from calculation, we expect that the iteration converges at x = 24.7386):
Notes[edit]
- ^Papakonstantinou, J., The Historical Development of the Secant Method in 1-D, retrieved 2011-06-29
See also[edit]
References[edit]
- Kaw, Autar; Kalu, Egwu (2008), Numerical Methods with Applications (1st ed.).
- Allen, Myron B.; Isaacson, Eli L. (1998). Numerical analysis for applied science. John Wiley & Sons. pp. 188–195. ISBN978-0-471-55266-6.
External links[edit]
- Secant Method Notes, PPT, Mathcad, Maple, Mathematica, Matlab at Holistic Numerical Methods Institute
- Weisstein, Eric W.'Secant Method'. MathWorld.
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Secant_method&oldid=891113740'
Hidden categories:
What is Secant Method?
The Secant Method is a root-finding algorithm that uses two initial approximations to start the iteration process. This root-finding algorithm uses a succession of roots of secant lines to better approximate a root of a function f(x) from to intial approximation x0 and x1.
|
---|
Intended Learning Outcome (What I will Learn?)
The readers should be able to:
- learn how to do solve the roots of a function f(x) by using Secant method;
- write a code to implement the Secant method algorithm in C++ programming language; and,
- run the code written in C++ for Secant method algorithm using Code::Blocks.
Requirements
To be able to follow this tutorial, you need to have the following tools:
- A desktop PC or laptop with Windows (7, 8, 10) operating system; and,
- An installed GCC Compiler and Code:: Blocks Open Source cross-platform IDE software.
If you do not have the following software, you can download the Code:: Blocks software from codeblocks.org. For the GCC Compiler, you can download a MinGW compiler from mingw.org.
Furthermore, it is advisable that you must read the other curriculum for the Numerical Method using C++ Programming. The links can be found on the curriculum section in this tutorial.
Difficulty
Secant method of iteration implemented in C++ Programming using Code::Blocks
Part 1: Setting up and writing the code for Newton-Raphson Algorithm
Setting up a New Project in Code::Blocks |
---|
Use the console application template in Code::Blocks to start a new project. Set in C++ as the programming language to be used. For the detailed instruction, feel free to read the the first part of Numerical Method using C++ Programming #1 For this tutorial, save the project as Secant Method. Then, go to Secant Method> Sources > main.cpp
|
Setting up function Secant() which contains the the Secant Method algorithm
1 As usual, start the program by declaring and defining standard libraries to be used. Hence we are doing a numerical algorithm, include mathematical standard library <cmath>
, and <iomanip>
for number precision. Write the proper syntax as follows:
#include <iostream>
#include <cmath>
#include <iosmanip>
using namespace std; |
---|
2 Declare the function for Secant(). Choose an appropriate data type and use the syntax data_type function( data_type identifier);
. Use a double data type for its flexibility to store characters, integers, and floating point as compared with integer and float data types. So, write your code according to the syntax structure. We will use three identifiers x, x0, and x1 for this tutorial. This identifiers will stroe the values of the root, and two values for our initial approximation.
double Secant(double x, double x0, double x1) |
---|
3 Define the function Secant() with the Secant method algorithm. Use this syntax to do so: double Secant(double x, double x0, double x1){ 'Secant_Method_Algorithm' }
.
4 Using a WHILE loop to translate and perform the Secant Method algorithm as indicated by the formula below.
Set WHILE loop condition that will satisfy both scenarios:
Scenario | Syntax used |
---|
1. Absolute vale of f(x1) should begreater than as compared to the error | fabs(data_type, indentifier)> e |
2. n approximations should be less than or equal to maximum iteration (Max_Iter) | n > = Max_Iter |
You have take note that you should properly define n approximation, error e and maximum iteration Max_iter. You need to set values for n, e, and Max_Iter as '2', '0.0001', and '100' respectively. However, you may change the values for e and Max_iter base on your discretion.
double Secant(double x, double x0, double x1){
int n =2;
const int Max_Iter=100;
const double e=0.0001; |
---|
Write the WHILE loop condition inside the bracket . Incorporate the scenarios mentioned earlier using the '&&' (AND) operator.
while( (fabs(double x1) > e) && ( n <= Max_Iter) |
---|
5 Define fx1 and fx0. For this tutorial, let have equation x^4 + x^2 = 0. So, use the syntax to pow(identifier, exponent) to get the result of raising the number to a certain exponent. This is the syntax
double fx0 = pow(x0,4) + pow(x,2);and
double fx0 = pow(x0,4) + pow(x,2);```, after identifier is being replaced.
6 Solve value of x using the formula as shown in step 4 . Writing down the formula as x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0);
.
7 Update values of x0 and x1. Use '=' operator change values stored at x0 and x1 to x1 and x respectively.
8 To complete, the While loop operation, put a iteration counter so that the loop will terminate at n = Max_Iter. Do it by adding '++' operator besides n.
9 Write down the complete code as describe in steps 4 to 8 in code::blocks.
double Secant(double x, double x0, double x1){
int n =2;
const int Max_Iter=100;
const double e=0.001;
while( (fabs(double x1) > e) && ( n <= Max_Iter)){
double fx0 = pow(x0,4) + pow(x,2);
double fx0 = pow(x0,4) + pow(x,2);
x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0);
x0=x1;
x1=x;
n++;
} return n; } |
---|
####Setting up I/O interface for the C++ program implementation of Secant Method (Method of Iteration )
10 Inside the main() function, write a code that will allow user to input initial values for iteration. As usual, use cout<<'Text to be print ' <<endl;
and ```cin>>'identifier/placeholder' for output and input syntax respectively.
int main(double x, double x0, double x1){
cout << 'Secant Method' << endl;
cout <<'Enter first initial approximation: ';
cin >>x0;
cout <<'Enter second initial approximation: '
cin >>x1;
cout <<'n The root of the equation is '<<x <<endl;
return 0; } |
---|
11 Build the program by clicking .
12 Run the program by clicking .
Here is the complete code of the program we have done.
#include <iostream>
#include <cmath>
#include <iosmanip>
using namespace std;
double Secant(double x, double x0, double x1){
int n =2;
const int Max_Iter=100;
const double e=0.001;
while( (fabs(double x1) > e) && ( n <= Max_Iter)){
double fx0 = pow(x0,4) + pow(x,2);
double fx0 = pow(x0,4) + pow(x,2);
x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0);
x0=x1;
x1=x;
n++;
} return n; }
int main(double x, double x0, double x1){
cout << 'Secant Method' << endl;
cout <<'Enter first initial approximation: ';
cin >>x0;
cout <<'Enter second initial approximation: '
cin >>x1;
cout <<'n The root of the equation is '<<x <<endl;
return 0; } |
---|
PART 2: Running the output program
1 Input a guess root. For example, i entered first and second initial approximation as 4 and 5 respectively.
2 Press ENTER to show result.
Curriculum
Here is the other tutorial for Numerical Methods on C++ series:
Posted on Utopian.io - Rewarding Open Source Contributors