#!/usr/bin/python3

import numpy as np
import matplotlib.pyplot as plt
import math as math

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

def diff(f,h,order,x): #returns the numerical derivative of a function
    if order > 1: #Higher order derivatives are defined recursively
        return (diff(f,h,order-1,x+h)-diff(f,h,order-1,x-h))/(2*h)
    else:
        return (f(x+h)-f(x-h))/(2*h)
    
def nrm(x,n): #Newton-Raphson method of n iterations using numerical derivative
    for i in range(0,n): #n iterations
        x = x - (f(x))/(diff(f,1e-9,1,x))
    return x

print(nrm(0.5,10))
