fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # 参数
  5. k = 5 * np.sqrt(3) / 3 # 由体积相等算出的 k
  6. sqrt3 = np.sqrt(3)
  7.  
  8. x = np.linspace(0, sqrt3, 400)
  9. y_parabola = x**2
  10. y_line = k * x
  11. y_top = 3 * np.ones_like(x)
  12.  
  13. # 交点横坐标
  14. x_inter = 3 / k # 直线与 y=3 的交点,此时约等于 1.039
  15. x_intersect_parabola = sqrt3 # 抛物线顶点与 y=3 交点
  16.  
  17. plt.figure(figsize=(6, 5))
  18.  
  19. # 填充区域 D (整体)
  20. plt.fill_between(x, y_parabola, y_top, where=(x <= sqrt3), color='lightgray', alpha=0.5, label='$D$ (整体)')
  21.  
  22. # 再分别用不同透明度区分 D1 和 D2
  23. # D2: 在 0 ≤ x ≤ x_inter 上,从直线到 y=3
  24. x2 = x[x <= x_inter]
  25. y2_lower = k * x2
  26. plt.fill_between(x2, y2_lower, 3, color='skyblue', alpha=0.7, label='$D_2$ (绕 x 轴旋转)')
  27. # D1: 分两段:0~x_inter 上从抛物线到直线;x_inter~sqrt3 上从抛物线到 y=3
  28. x1a = x[x <= x_inter]
  29. y1a_lower = x1a**2
  30. y1a_upper = k * x1a
  31. x1b = x[(x >= x_inter) & (x <= sqrt3)]
  32. y1b_lower = x1b**2
  33. plt.fill_between(x1a, y1a_lower, y1a_upper, color='lightcoral', alpha=0.7, label='$D_1$ (绕 x 轴旋转)')
  34. plt.fill_between(x1b, y1b_lower, 3, color='lightcoral', alpha=0.7)
  35.  
  36. # 绘制曲线
  37. plt.plot(x, y_parabola, 'b-', linewidth=2, label='$y = x^2$')
  38. plt.plot(x, y_line, 'r--', linewidth=2, label=f'$y = kx,\ k = {k:.3f}$')
  39. plt.axhline(y=3, color='green', linestyle='--', linewidth=2, label='$y = 3$')
  40. plt.axvline(x=0, color='black', linewidth=0.5)
  41.  
  42. # 标注交点
  43. plt.plot([x_inter, x_inter], [0, 3], 'k:', linewidth=0.8)
  44. plt.plot(x_inter, 3, 'ko', markersize=5)
  45. plt.plot(sqrt3, 3, 'ko', markersize=5)
  46. plt.plot(0, 0, 'ko', markersize=3)
  47.  
  48. plt.xlim(0, sqrt3 + 0.2)
  49. plt.ylim(0, 3.2)
  50. plt.xlabel('$x$')
  51. plt.ylabel('$y$')
  52. plt.title('区域 $D$ 被直线 $y=kx$ 分成 $D_1$ 和 $D_2$')
  53. plt.legend(loc='upper left')
  54. plt.grid(alpha=0.3)
  55. plt.gca().set_aspect('equal', adjustable='box')
  56. plt.show()
Success #stdin #stdout 1.19s 58656KB
stdin
Standard input is empty
stdout
Standard output is empty