The secant method is used to find the root of an equation f(x) = 0. It is started from two distinct estimates x1 and x2 for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if the difference between two intermediate values is less than the convergence factor.
Examples :
Input : equation = x3 + x – 1
x1 = 0, x2 = 1, E = 0.0001
Output : Root of the given equation = 0.682326
No. of iteration=5
Algorithm
Initialize: x1, x2, E, n // E = convergence indicator
calculate f(x1),f(x2)
if(f(x1) * f(x2) = E); //repeat the loop until the convergence
print 'x0' //value of the root
print 'n' //number of iteration
}
else
print "can not found a root in the given interval"
C++
#include <bits/stdc++.h>
using namespace std;
float f( float x)
{
float f = pow (x, 3) + x - 1;
return f;
}
void secant( float x1, float x2, float E)
{
float n = 0, xm, x0, c;
if (f(x1) * f(x2) < 0) {
do {
x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0)
break ;
xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
} while ( fabs (xm - x0) >= E);
cout << "Root of the given equation=" << x0 << endl;
cout << "No. of iterations = " << n << endl;
} else
cout << "Can not find a root in the given interval" ;
}
int main()
{
float x1 = 0, x2 = 1, E = 0.0001;
secant(x1, x2, E);
return 0;
}
|
Java
class GFG {
static float f( float x) {
float f = ( float )Math.pow(x, 3 )
+ x - 1 ;
return f;
}
static void secant( float x1, float x2,
float E) {
float n = 0 , xm, x0, c;
if (f(x1) * f(x2) < 0 )
{
do {
x0 = (x1 * f(x2) - x2 * f(x1))
/ (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0 )
break ;
xm = (x1 * f(x2) - x2 * f(x1))
/ (f(x2) - f(x1));
} while (Math.abs(xm - x0) >= E);
System.out.println( "Root of the" +
" given equation=" + x0);
System.out.println( "No. of "
+ "iterations = " + n);
}
else
System.out.print( "Can not find a"
+ " root in the given interval" );
}
public static void main(String[] args) {
float x1 = 0 , x2 = 1 , E = 0 .0001f;
secant(x1, x2, E);
}
}
|
Python3
def f(x):
f = pow (x, 3 ) + x - 1 ;
return f;
def secant(x1, x2, E):
n = 0 ; xm = 0 ; x0 = 0 ; c = 0 ;
if (f(x1) * f(x2) < 0 ):
while True :
x0 = ((x1 * f(x2) - x2 * f(x1)) /
(f(x2) - f(x1)));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n + = 1 ;
if (c = = 0 ):
break ;
xm = ((x1 * f(x2) - x2 * f(x1)) /
(f(x2) - f(x1)));
if ( abs (xm - x0) < E):
break ;
print ( "Root of the given equation =" ,
round (x0, 6 ));
print ( "No. of iterations = " , n);
else :
print ( "Can not find a root in " ,
"the given interval" );
x1 = 0 ;
x2 = 1 ;
E = 0.0001 ;
secant(x1, x2, E);
|
C#
using System;
class GFG {
static float f( float x)
{
float f = ( float )Math.Pow(x, 3)
+ x - 1;
return f;
}
static void secant( float x1, float x2,
float E)
{
float n = 0, xm, x0, c;
if (f(x1) * f(x2) < 0)
{
do {
x0 = (x1 * f(x2) - x2 * f(x1))
/ (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0)
break ;
xm = (x1 * f(x2) - x2 * f(x1))
/ (f(x2) - f(x1));
} while (Math.Abs(xm - x0) >= E);
Console.WriteLine( "Root of the" +
" given equation=" + x0);
Console.WriteLine( "No. of " +
"iterations = " + n);
}
else
Console.WriteLine( "Can not find a" +
" root in the given interval" );
}
public static void Main(String []args)
{
float x1 = 0, x2 = 1, E = 0.0001f;
secant(x1, x2, E);
}
}
|
PHP
<?php
function f( $x )
{
$f = pow( $x , 3) + $x - 1;
return $f ;
}
function secant( $x1 , $x2 , $E )
{
$n = 0; $xm ;
$x0 ; $c ;
if (f( $x1 ) * f( $x2 ) < 0)
{
do {
$x0 = ( $x1 * f( $x2 ) - $x2 *
f( $x1 )) / (f( $x2 ) - f( $x1 ));
$c = f( $x1 ) * f( $x0 );
$x1 = $x2 ;
$x2 = $x0 ;
$n ++;
if ( $c == 0)
break ;
$xm = ( $x1 * f( $x2 ) - $x2 * f( $x1 )) /
(f( $x2 ) - f( $x1 ));
} while ( abs ( $xm - $x0 ) >= $E );
echo "Root of the given equation=" . $x0 . "\n" ;
echo "No. of iterations = " . $n ;
} else
echo "Can not find a root in the given interval" ;
}
{
$x1 = 0; $x2 = 1;
$E = 0.0001;
secant( $x1 , $x2 , $E );
return 0;
}
?>
|
Javascript
<script>
function f(x)
{
let f = Math.pow(x, 3) + x - 1;
return f;
}
function secant(x1, x2, E)
{
let n = 0, xm, x0, c;
if (f(x1) * f(x2) < 0) {
do {
x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0)
break ;
xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
} while (Math.abs(xm - x0) >= E);
document.write( "Root of the given equation=" + x0.toFixed(6) + "<br>" );
document.write( "No. of iterations = " + n + "<br>" );
} else
document.write( "Can not find a root in the given interval" );
}
let x1 = 0, x2 = 1, E = 0.0001;
secant(x1, x2, E);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference
https://en.wikipedia.org/wiki/Secant_method
This article is contributed by Niteesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.