#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug  6 15:48:09 2024

@author: sam
"""

import numpy as np
import matplotlib.pyplot as plt



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



h = np.logspace(-10,-5,100)

xvalues = [0.1,0.5,1,1.5]

for x in xvalues:
    dfdx = (f(x+h)-f(x))/h
    realdfdx = 2*np.sin(x)*np.cos(x)
    
    abs_err = abs(dfdx-realdfdx)
    rel_err = abs(abs_err/realdfdx)
    
    #plt.loglog(h,abs_err,'.')
    plt.loglog(h,rel_err,'.')
    
    plt.show()