Implementation of Secant Method written in Python.

IN
-3
-1
0.0000001
OUT
X:  -3.0  f(x):  2.0
X:  -2.0  f(x):  -1.0
X:  -3.0  f(x):  2.0
X:  -2.3333333333333335  f(x):  -0.22222222222222188
X:  -2.4  f(x):  -0.04000000000000026
X:  -2.4146341463414633  f(x):  0.0011897679952406204
X:  -2.41421143847487  f(x):  -6.007286838749515e-06
# © 2018 TheFlyingKeyboard and released under MIT License
# theflyingkeyboard.net
def function(x):
    return x * (x + 2) - 1

xn1 = float(input("Enter begining of interval: "))
xn = float(input("Enter end of interval: "))
precision = float(input("Enter precision of method: "))

x = xn1

if(function(xn1) * function(xn) < 0):
  while(abs(function(x)) > precision):
    print("X: ", x, " f(x): ", function(x))

    x = xn - ((function(xn) * (xn - xn1)) / (function(xn) - function(xn1)))
    xn1 = xn
    xn = x

 



Python Secant Method
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close