割线法用于求方程f(x)=0的根。它从根的两个不同估计值x1和x2开始。这是一个迭代过程,涉及到根的线性插值。如果两个中间值之间的差值小于收敛因子,则迭代停止。
null
例如:
Input : equation = x3 + x - 1 x1 = 0, x2 = 1, E = 0.0001Output : Root of the given equation = 0.682326 No. of iteration=5
算法
Initialize: x1, x2, E, n // E = convergence indicatorcalculate 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++
// C++ Program to find root of an // equations using secant method #include <bits/stdc++.h> using namespace std; // function takes value of x and returns f(x) float f( float x) { // we are taking equation as x^3+x-1 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 { // calculate the intermediate value x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // check if x0 is root of equation or not c = f(x1) * f(x0); // update the value of interval x1 = x2; x2 = x0; // update number of iteration n++; // if x0 is the root of equation then break the loop if (c == 0) break ; xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); } while ( fabs (xm - x0) >= E); // repeat the loop // until the convergence 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" ; } // Driver code int main() { // initializing the values float x1 = 0, x2 = 1, E = 0.0001; secant(x1, x2, E); return 0; } |
JAVA
// Java Program to find root of an // equations using secant method class GFG { // function takes value of x and // returns f(x) static float f( float x) { // we are taking equation // as x^3+x-1 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 { // calculate the intermediate // value x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // check if x0 is root of // equation or not c = f(x1) * f(x0); // update the value of interval x1 = x2; x2 = x0; // update number of iteration n++; // if x0 is the root of equation // then break the loop if (c == 0 ) break ; xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // repeat the loop until the // convergence } 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" ); } // Driver code public static void main(String[] args) { // initializing the values float x1 = 0 , x2 = 1 , E = 0 .0001f; secant(x1, x2, E); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 Program to find root of an # equations using secant method # function takes value of x # and returns f(x) def f(x): # we are taking equation # as x^3+x-1 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 : # calculate the intermediate value x0 = ((x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1))); # check if x0 is root of # equation or not c = f(x1) * f(x0); # update the value of interval x1 = x2; x2 = x0; # update number of iteration n + = 1 ; # if x0 is the root of equation # then break the loop 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" ); # Driver code # initializing the values x1 = 0 ; x2 = 1 ; E = 0.0001 ; secant(x1, x2, E); # This code is contributed by mits |
C#
// C# Program to find root of an // equations using secant method using System; class GFG { // function takes value of // x and returns f(x) static float f( float x) { // we are taking equation // as x^3+x-1 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 { // calculate the intermediate // value x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // check if x0 is root of // equation or not c = f(x1) * f(x0); // update the value of interval x1 = x2; x2 = x0; // update number of iteration n++; // if x0 is the root of equation // then break the loop if (c == 0) break ; xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // repeat the loop until // the convergence } 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" ); } // Driver code public static void Main(String []args) { // initializing the values float x1 = 0, x2 = 1, E = 0.0001f; secant(x1, x2, E); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to find root of an // equations using secant method // function takes value of x // and returns f(x) function f( $x ) { // we are taking equation // as x^3+x-1 $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 { // calculate the intermediate value $x0 = ( $x1 * f( $x2 ) - $x2 * f( $x1 )) / (f( $x2 ) - f( $x1 )); // check if x0 is root // of equation or not $c = f( $x1 ) * f( $x0 ); // update the value of interval $x1 = $x2 ; $x2 = $x0 ; // update number of iteration $n ++; // if x0 is the root of equation // then break the loop if ( $c == 0) break ; $xm = ( $x1 * f( $x2 ) - $x2 * f( $x1 )) / (f( $x2 ) - f( $x1 )); // repeat the loop // until the convergence } while ( abs ( $xm - $x0 ) >= $E ); echo "Root of the given equation=" . $x0 . "" ; echo "No. of iterations = " . $n ; } else echo "Can not find a root in the given interval" ; } // Driver code { // initializing the values $x1 = 0; $x2 = 1; $E = 0.0001; secant( $x1 , $x2 , $E ); return 0; } // This code is contributed by nitin mittal. ?> |
Javascript
<script> // JavaScript Program to find root of an // equations using secant method // function takes value of x and returns f(x) function f(x) { // we are taking equation as x^3+x-1 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 { // calculate the intermediate value x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); // check if x0 is root of equation or not c = f(x1) * f(x0); // update the value of interval x1 = x2; x2 = x0; // update number of iteration n++; // if x0 is the root of equation then break the loop if (c == 0) break ; xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); } while (Math.abs(xm - x0) >= E); // repeat the loop // until the convergence 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" ); } // Driver code // initializing the values let x1 = 0, x2 = 1, E = 0.0001; secant(x1, x2, E); // This code is contributed by Surbhi Tyagi. </script> |
输出:
Root of the given equation = 0.682326No. of iterations = 5
时间复杂度=O(1)
参考 https://en.wikipedia.org/wiki/Secant_method 本文由 尼蒂什·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END