[text] ASTR

Viewer

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def rho(r, rho_c, R_star):
  5.     return rho_c * ((r/R_star) - 1)**2
  6.  
  7. def M(r, rho_c, R_star):
  8.     integrand = lambda r_prime: 4*np.pi*r_prime**2 * rho(r_prime, rho_c, R_star)
  9.     result, _ = np.trapz(integrand(np.linspace(0, r, 1000)), np.linspace(0, r, 1000))
  10.     return result
  11.  
  12. rho_c = 1.0  # Central density
  13. R_star = 1.0  # Radius of the star
  14.  
  15. r_values = np.linspace(0, R_star, 100)
  16. M_values = [M(r, rho_c, R_star) for r in r_values]
  17.  
  18. plt.plot(r_values, M_values)
  19. plt.xlabel('Radius (r)')
  20. plt.ylabel('Enclosed Mass (M(r))')
  21. plt.title('Enclosed Mass vs Radius')
  22. plt.grid(True)
  23. plt.show()
  24.  

Editor

You can edit this paste and save as new:


File Description
  • ASTR
  • Paste Code
  • 29 Mar-2024
  • 663 Bytes
You can Share it: