#!/usr/bin/python3

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

def f(x): #sin(x)^2
    return np.sin(x)**2

def diff(f,h,x): #returns the numerical derivative of a function
    return (f(x+h)-f(x))/h

x = np.logspace(-1,1) #set domain as [-1,1]

plt.loglog(x,abs(np.sin(2*x)-diff(f,1e-9,x))) #plot absolute error of f'(x)
plt.loglog(x,diff(f,1e-9,x)/np.sin(2*x)) #plot f'(x)
plt.show()

