# -*- coding: utf-8 -*-
from numpy import linspace
import math

def secant (f,x1,x2,tol=0.001,maxiter=50):
    for iteration in range(maxiter):
        xnew = x2- (x2-x1)/(f(x2)-f(x1))*f(x2)
        if abs(xnew-x2)<tol: break
        else:
            x1=x2
            x2=xnew
            
    else:
        print("Warning:Maximum number of iterations is reached!")
    return xnew, iteration

def f(x): return math.exp(-x)-x
x1= 0
x2= 1


if __name__ == "__main__":
    if True: 
        xs = linspace( -2, +2, 100 ) 
        fs = [ f(_) for _ in xs ]
    
        import pylab; #It works in the same logic as "import matplotlib.pyplot as plt ".There is only a spelling difference.
        pylab.plot( xs, fs ) 
        pylab.xlabel('x')
        pylab.ylabel('f(x)')
        pylab.axhline(y=0,color='k')
        pylab.grid() 
        pylab.show()

print(secant (f,x1,x2))





