#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 14:59:32 2024

@author: sam
"""

import numpy as np

'''define functions'''
def bisection(f,a,b,tol):
    while abs(f(b) - f(a)) >= tol:
        c = (a+b)/2
        if f(a)*f(c) > 0:
            a = c
            #print('took right side')
        elif f(c)*f(b) > 0:
            b = c
            #print('took left side')
        else:
            print('Fail')
            break
    return (a+b)/2

def f(x):
    f = x - np.cos(x)
    return f



#run "mysearch" procedure
a = 0
b = 1
tol = 0.0001
x = bisection(f,a,b,tol)
print('Zero found at',x)