Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. # First we make a function for the Trapezoid rule
  2. def trapezoid(f, a, b, N):
  3. n = 0
  4. sum = 0
  5. d = (b-a)/N
  6. xi = a
  7. xj = a+d
  8. while n < N:
  9. sum += (b-a)/(2*N)*(f(xi) + f(xj))
  10. xi += d
  11. xj += d
  12. n += n
  13. return sum
  14.  
  15. # Secondly we make a function for Simpson's rule
  16. def simpson(f,a,b,N):
  17. n = 0
  18. sum = 0
  19. d = (b-a)/N
  20. xi = a
  21. xj = a+d
  22. while n < N:
  23. sum += (b-a)/(6*N)*(f(xi) + 4*f((xi + xj)/2) +f(xj))
  24. xi += d
  25. xj += d
  26. n += n
  27. return sum
  28.  
  29. # These two functions are fine and all, but it helpful to defin two more functions for these
  30. # two functions that allow us to lot them for later. Further more, this function will call
  31. # the simple() command with a given value of k in order to minimize error with array while in the loop
  32.  
  33. def formatDataTrap(f,a,b,k):
  34. resultArray = np.array([])
  35. for k in k:
  36. result = trapezoid(f,a,b,k)
  37. resultArray = np.append(resultArray, result)
  38. return resultArray
  39.  
  40. def formatDataSimpson(f,a,b,k):
  41. resultArray = np.array([])
  42. for k in k:
  43. result = simpson(f,a,b,k)
  44. resultArray = np.append(resultArray, result)
  45. return resultArray
  46.  
  47.  
  48. # First we work with t^3
  49. f1 = lambda x: x**3
  50. true1 = 1/4 #This is what the actual integral evaluates to
  51. i = np.arange(2, 20, 1)
  52. k = 2**i
  53. eps = np.finfo(np.double).eps
  54.  
  55. errorTrap = lambda k: -np.log10(abs(true1-formatDataTrap(f1,0,1,k))+eps)
  56. errorSimpson = lambda k: -np.log10(abs(true1-formatDataSimpson(f1,0,1,k))+eps)
  57.  
  58. plt.plot(k, errorTrap(k), "r", k, errorSimpson(k), 'b')
  59. plt.xlabel('N spacings')
  60. plt.ylabel('Negative Logarithmic Error')
  61. plt.title('Error for the function x**3')
  62. plt.show()
  63.  
  64.  
  65.  
  66. # Since the method is roughly the same for the other two functions,
  67. # we can easily copy and paste the code to solve for each of these.
  68.  
  69. f2 = lambda x: 1/(1+x**2)
  70. true2 = 2*np.arctan(2) #This is what the actual integral evaluates to
  71.  
  72. errorTrap = lambda k: -np.log10(abs(true2-formatDataTrap(f2,-2,2,k))+eps)
  73. errorSimpson = lambda k: -np.log10(abs(true2-formatDataSimpson(f2,-2,2,k))+eps)
  74.  
  75. plt.plot(k, errorTrap(k), "r", k, errorSimpson(k), 'b')
  76. plt.xlabel('N spacings')
  77. plt.ylabel('Negative Logarithmic Error')
  78. plt.title('Error for the function 1/(1+x**2)')
  79. plt.show()
  80.  
  81.  
  82. f3 = lambda x: np.cos(10**5*x)
  83. true3 = 10**(-5)*np.sin(10**5) #This is what the actual integral evaluates to
  84.  
  85. errorTrap = lambda k: -np.log10(abs(true3-formatDataTrap(f3, 0, 1,k))+eps)
  86. errorSimpson = lambda k: -np.log10(abs(true3-formatDataSimpson(f3,0,1,k))+eps)
  87.  
  88. plt.plot(k, errorTrap(k), "r", k, errorSimpson(k), 'b')
  89. plt.xlabel('N spacings')
  90. plt.ylabel('Negative Logarithmic Error')
  91. plt.title('Error for the function cos(10**5*x)')
  92. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement