Guest User

Discrete Integration with Overlapping Partitions of Fixed Width

a guest
Mar 26th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | Science | 0 0
  1. # Discrete Integration with Overlapping Partitions of Fixed Width
  2. # Uses:
  3. # LP Positions on an AMM
  4. # Solid to Liquid transition Ice >> Water
  5. # Bose-Einstein condensate converting to Fermi-Dirac statistics
  6.  
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. from scipy.special import erf
  10.  
  11. def f(x):
  12.     return np.exp(-((x - 10)**2) / 10)
  13.  
  14. a, b = 3, 19  # Discrete Integration Bounds
  15. width = 3 # our constraint
  16. step = 0.5 # our discrete grid
  17. x_starts = np.arange(a-step, b + step, step)
  18.  
  19. # Build LP Positions
  20. rectangles = []
  21. for x in x_starts:
  22.     base = sum(r[2] for r in rectangles if r[0] <= x < r[1])
  23.     height = max(0, f(x) - base)
  24.     rectangles.append((x, x + width, height, base))
  25.  
  26. # Compute total area
  27. total_area = sum(height * width for _, _, height, _ in rectangles)
  28. exact_area = np.sqrt(10 * np.pi) / 2 * (erf(9 / np.sqrt(10)) + erf(7 / np.sqrt(10)))
  29.  
  30. # Cumulative height data
  31. x_points = np.arange(min(x_starts), max(x_starts) + width, step)
  32. heights = [sum(r[2] for r in rectangles if r[0] <= x < r[1]) for x in x_points]
  33.  
  34. # Midpoint Shift for plotting only
  35. shift = step / 2
  36. x_points_shifted = x_points - shift
  37.  
  38. plt.figure(figsize=(14, 7))
  39. x_curve = np.linspace(a - 3, b + 3, 200)
  40. y_curve = f(x_curve)
  41. plt.plot(x_curve, y_curve, 'b-', label='f(x) = e^(-(x-10)²/10)', linewidth=2.5)
  42. plt.step(x_points_shifted, heights, where='post', color='r', label='Cumulative Height (Shifted Step/2)', linewidth=2)
  43.  
  44. # Rectangles from y=0 (shifted)
  45. for i, (x0, x1, height, _) in enumerate(rectangles):
  46.     if height > 0:
  47.         color = plt.cm.viridis(i / len(rectangles))
  48.         plt.gca().add_patch(
  49.             plt.Rectangle((x0 - shift, 0), width, height, alpha=0.6, facecolor=color,
  50.                           edgecolor='k', linewidth=0.8,
  51.                           label=f'LP Positions' if i == 0 else None)
  52.         )
  53.  
  54. plt.title(f'Centered Stacking with Midpoint\nApprox Area = {total_area:.4f}, Exact = {exact_area:.4f}',
  55.           fontsize=14, pad=10)
  56. plt.xlabel('x', fontsize=12)
  57. plt.ylabel('y', fontsize=12)
  58. plt.xlim(a - 3, b + 3)
  59. plt.ylim(0, 1.2)
  60. plt.grid(True, linestyle='--', alpha=0.5)
  61. plt.legend(loc='upper left', fontsize=10)
  62. plt.tight_layout()
  63. plt.show()
  64.  
  65. print("Sample rectangles (unshifted positions):")
  66. for x0, x1, h, base in rectangles[:5]:
  67.     print(f"Rect from {x0} to {x1}: height = {h:.4f}, base = {base:.4f}, top = {base + h:.4f}")
  68. print(f"Rectangles near peak (unshifted positions):")
  69. for x0, x1, h, base in rectangles[15:20]:
  70.     print(f"Rect from {x0} to {x1}: height = {h:.4f}, base = {base:.4f}, top = {base + h:.4f}")
  71. print(f"Total area: {total_area:.4f}, Exact: {exact_area:.4f}")
Tags: integration
Advertisement
Add Comment
Please, Sign In to add comment