Guest User

Untitled

a guest
Feb 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. def double_integral(func, limits, res=1000):
  2. t = time.clock()
  3. t1 = time.clock()
  4. t2 = time.clock()
  5. s = 0
  6. a, b = limits[0], limits[1]
  7. outer_values = np.linspace(a, b, res)
  8. c_is_func = callable(limits[2])
  9. d_is_func = callable(limits[3])
  10. for y in outer_values:
  11. if c_is_func:
  12. c = limits[2](y)
  13. else:
  14. c = limits[2]
  15. if d_is_func:
  16. d = limits[3](y)
  17. else:
  18. d = limits[3]
  19. dA = ((b - a) / res) * ((d - c) / res)
  20. inner_values = np.linspace(c, d, res)
  21. for x in inner_values:
  22. t2 = time.clock() - t2
  23. s += func(x, y) * dA
  24. t1 = time.clock() - t1
  25. t = time.clock() - t
  26. return s, t, t1 / res, t2 / res**2
  27.  
  28. def f(x, y):
  29. if (4 - y**2 - x**2) < 0:
  30. return 0 #This is to avoid taking the root of negarive #'s
  31. return np.sqrt(4 - y**2 - x**2)
  32.  
  33. def c(y):
  34. return np.sqrt(2 * y - y**2)
  35.  
  36. def d(y):
  37. return np.sqrt(4 - y**2)
  38. # b d
  39. # S S f(x,y) dx dy
  40. # a c
  41. a, b, = 0, 2
  42. print(double_integral(f, [a, b, c, d]))
Add Comment
Please, Sign In to add comment