#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug  6 16:35:40 2024

@author: sam
"""

import numpy as np
import matplotlib.pyplot as plt



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



h = np.logspace(-8,-1,100)

xvalues = [0.1,0.5,1,1.5]

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