#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 15:55:34 2024

@author: sam
"""

import numpy as np

roots = [] #create list

'''define functions'''
def f(x):
    return x**4 - x - 1

def bracket(f,a,b,N):
    x = np.linspace(a,b,N)
    
    for i in range(len(x)-1):
        if f(x[i])*f(x[i+1])<0:
            roots.append([x[i],x[i+1]])
            
    '''for i in range(-2000,2000):
        d = i/1000
        e = (i+1)/1000
        if f(d)*f(e) < 0: #check for change in sign
            roots.append([d,e]) #add bracketed roots to list
        else:
            continue
        ''' #old method

def secant(f,a,b,tol):
    while abs(a-b) >= tol:
        c = (b*f(a)-a*f(b))/(f(a)-f(b))
        b = a
        a = c
        
        '''c = (b*f(a)-a*f(b))/(f(a)-f(b))
        if f(a)*f(c) > 0:
            a = c
        elif f(c)*f(b) > 0:
            b = c
        else:
            continue
        ''' #old method
    return c



bracket(f,-2,2,2000)
for i in range(len(roots)): #print roots individually
    print(roots[i])

for i in range(len(roots)):
        [a,b] = roots[i][:]
        x0 = secant(f,a,b,1e-4)
        print("Zero found at:",x0)