Guest User

Untitled

a guest
Apr 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import numpy
  2. import matplotlib.pyplot as plt
  3.  
  4. a = 0
  5. b = 1
  6.  
  7. c = -1
  8. d = 1
  9.  
  10. def f(x):
  11. return (3 * x ** 3 * numpy.arcsin(x) + x**2 * numpy.sqrt(1 - x**2) + 2 * numpy.sqrt(1 - x**2) - 2) / (9 * x ** 3)
  12.  
  13. def integrate(x, n):
  14. sum = 0
  15. for t in numpy.linspace((b-a)/n, b, n-1):
  16. sum += t**2 * numpy.arcsin(x * t)
  17. return sum * (b-a)/n
  18.  
  19. def calculate_max_diff(Y1, Y2):
  20. return max(abs(Y1-Y2))
  21.  
  22. points_num = 20
  23. N = [5, 10, 20, 50, 100, 200]
  24. Y_approx = []
  25. max_diff = []
  26.  
  27. X = numpy.linspace(c, d, points_num).reshape(-1, 1)
  28. Y = f(X)
  29.  
  30. for i in range(len(N)):
  31. Y_approx.append(integrate(X, N[i]))
  32. max_diff.append(calculate_max_diff(Y, Y_approx[i]))
  33.  
  34. fig = plt.figure()
  35. ax1 = fig.add_subplot(111)
  36.  
  37. ax1.scatter(X, Y, label='original')
  38. for i in range(len(N)):
  39. ax1.plot(X, Y_approx[i], label=str(N[i]) + ' diff = ' + str(max_diff[i]))
  40. plt.legend(loc='upper left')
  41. plt.show()
Add Comment
Please, Sign In to add comment