fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # Graph A: r^2 = 4cos(2theta)
  5. theta_a = np.linspace(-np.pi/4, np.pi/4, 500)
  6. theta_a = np.concatenate((theta_a, np.linspace(3*np.pi/4, 5*np.pi/4, 500)))
  7. r_a_pos = 2 * np.sqrt(np.cos(2 * theta_a))
  8. r_a_neg = -2 * np.sqrt(np.cos(2 * theta_a))
  9.  
  10. # Graph B: r^2 = sin(2theta)
  11. theta_b = np.linspace(0, np.pi/2, 500)
  12. theta_b = np.concatenate((theta_b, np.linspace(np.pi, 3*np.pi/2, 500)))
  13. # Ensure sin(2*theta_b) is non-negative for sqrt
  14. sin_2theta_b = np.sin(2 * theta_b)
  15. valid_indices_b = sin_2theta_b >= 0
  16. r_b_pos = np.full_like(theta_b, np.nan)
  17. r_b_neg = np.full_like(theta_b, np.nan)
  18. r_b_pos[valid_indices_b] = np.sqrt(sin_2theta_b[valid_indices_b])
  19. r_b_neg[valid_indices_b] = -np.sqrt(sin_2theta_b[valid_indices_b])
  20.  
  21.  
  22. # Graph C: r = 2cos(3theta/2)
  23. theta_c = np.linspace(0, 4 * np.pi, 500)
  24. r_c = 2 * np.cos(3 * theta_c / 2)
  25.  
  26. # Graph D: r^2 * theta = 1 => r = +/- 1/sqrt(theta)
  27. theta_d = np.linspace(0.01, 4 * np.pi, 500) # theta > 0
  28. r_d_pos = 1 / np.sqrt(theta_d)
  29. r_d_neg = -1 / np.sqrt(theta_d)
  30.  
  31. # Graph E: r = 1 + 2cos(2theta)
  32. theta_e = np.linspace(0, 2 * np.pi, 500)
  33. r_e = 1 + 2 * np.cos(2 * theta_e)
  34.  
  35. # Graph F: r = 1 + 2cos(theta/2)
  36. theta_f = np.linspace(0, 4 * np.pi, 500)
  37. r_f = 1 + 2 * np.cos(theta_f / 2)
  38.  
  39. # Plotting
  40. fig, axs = plt.subplots(2, 3, subplot_kw={'projection': 'polar'}, figsize=(15, 10))
  41. fig.suptitle('Graphs of Polar Equations', fontsize=16)
  42.  
  43. # A
  44. axs[0, 0].plot(theta_a, r_a_pos, color='blue')
  45. axs[0, 0].plot(theta_a, r_a_neg, color='blue')
  46. axs[0, 0].set_title(r'A. $r^2 = 4\cos(2\theta)$', fontsize=10)
  47. axs[0, 0].set_rticks([-2, -1, 0, 1, 2])
  48.  
  49. # B
  50. axs[0, 1].plot(theta_b, r_b_pos, color='red')
  51. axs[0, 1].plot(theta_b, r_b_neg, color='red')
  52. axs[0, 1].set_title(r'B. $r^2 = \sin(2\theta)$', fontsize=10)
  53. axs[0, 1].set_rticks([-1, -0.5, 0, 0.5, 1])
  54.  
  55.  
  56. # C
  57. axs[0, 2].plot(theta_c, r_c, color='green')
  58. axs[0, 2].set_title(r'C. $r = 2\cos(3\theta/2)$', fontsize=10)
  59.  
  60. # D
  61. axs[1, 0].plot(theta_d, r_d_pos, color='purple')
  62. axs[1, 0].plot(theta_d, r_d_neg, color='purple')
  63. axs[1, 0].set_title(r'D. $r^2\theta = 1$', fontsize=10)
  64. axs[1, 0].set_rlim(-5,5)
  65.  
  66.  
  67. # E
  68. axs[1, 1].plot(theta_e, r_e, color='orange')
  69. axs[1, 1].set_title(r'E. $r = 1+2\cos(2\theta)$', fontsize=10)
  70.  
  71. # F
  72. axs[1, 2].plot(theta_f, r_f, color='brown')
  73. axs[1, 2].set_title(r'F. $r = 1+2\cos(\theta/2)$', fontsize=10)
  74.  
  75. for ax_row in axs:
  76. for ax in ax_row:
  77. ax.grid(True)
  78. ax.set_theta_zero_location("N") # North
  79. ax.set_theta_direction(-1) # Clockwise
  80.  
  81. plt.tight_layout(rect=[0, 0, 1, 0.96]) # Adjust layout to make space for suptitle
  82. plt.show()
Success #stdin #stdout 1.42s 76764KB
stdin
Standard input is empty
stdout
Standard output is empty