Python Forum

Full Version: linspace not dividing equal intervals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have written a code to find out effect of order of accuracy on numerical differentiation but when i am trying to use linspace(1,epsilon,100) it is not dividing it into equal interval.here is the code and the plot for the error is also incorrect

from numpy import *
import sys
import numpy as np
x=1
epsilon=sys.float_info.epsilon/2.0
# machine epsilon
h = np.linspace(1,epsilon,1000)
e1= (((sin(x)- sin(x-h))/h-cos(x))/cos(x))                                    #realtive error in first order
e2= (((sin(x-2*h)-4*sin(x-h)+3*sin(x))/(2*h)-cos(x))/cos(x))                 #realtive error in second order
e3= (((2*sin(x+h)+3*sin(x)-6*sin(x-h)+sin(x-2*h))/(6*h)-cos(x))/cos(x))      #realtive error in third order
e4 = (((-sin(x-3*h)+6*sin(x-2*h)-18*sin(x-h)+10*sin(x)+3*sin(x+h))/(12*h)-cos(x))/cos(x)) 
print(h)
import matplotlib.pyplot as plt
plt.plot(h,e1,'-b',lw=3)
plt.plot(h,e2,'-g',lw=3)
plt.plot(h,e3,'-m',lw=3)
plt.plot(h,e4,'-r',lw=3)
plt.xscale('log')
plt.yscale('log')
plt.xlabel('h')
plt.ylabel('Relative error')
plt.legend(['first order','second order','third order','fourth order'])
plt.title('Effect of order of accuracy on the numerical differentiation')
plt.show()