Python Forum
linspace not dividing equal intervals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
linspace not dividing equal intervals
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 487 Mar-14-2020, 09:19 AM
Last Post: scidam
  datetime intervals - dataframe selection (via plotted data) karlito 0 350 Nov-12-2019, 08:16 AM
Last Post: karlito
  When dividing elements of matrix i would like the result 0 instead of inf? Koczkodan 4 809 Jul-22-2019, 11:40 AM
Last Post: Koczkodan
  Confidence intervals over time with polynomial regression jjameson 0 953 May-16-2019, 02:22 PM
Last Post: jjameson
  dividing by list not possible? SchroedingersLion 5 1,168 Oct-11-2018, 10:00 AM
Last Post: volcano63
  RNG; One variable must not be equal to another DavidRobinsons 12 1,958 Oct-10-2018, 02:43 AM
Last Post: micseydel
  How to extract previous and current rows if their value is equal SriRajesh 5 1,575 Jun-18-2018, 08:09 PM
Last Post: volcano63
  Conditional If Statement: If value contains string then set another column equal to s Jack_Sparrow 2 2,245 Jun-15-2018, 03:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020