Wednesday, 29 April 2026

Speeding Galaxies

Introduction 

Universe is not a static thing. It is dynamic.  We all know galaxies are still moving away as the universe is expanding.  
We took data provided by NASA for educational purposes for 7 galaxies.  It consisted of names, distances,and speeds of the galaxies. 
The data was plotted on XY plot and linear regression analysis were done. Plotting was done using numpy library and matplotlib. 
Coding was done in python language. 
One obvious conclusion from the plots is that more distant galaxies are moving with more speed. 



import numpy as np
import matplotlib.pyplot as plt

# 1. Constants & Data
ONE_MPC_LY = 3.26 
galaxies = {
    "NGC-5357": (0.45, 200), "NGC-3627": (0.9, 650), "NGC-5236": (0.9, 500),
    "NGC-4151": (1.7, 960),  "NGC-4472": (2.0, 850), "NGC-4486": (2.0, 800),
    "NGC-4649": (2.0, 1090),
}

# Convert data for calculation
dist_mpc = np.array([d[0] for d in galaxies.values()])
speed_kms = np.array([d[1] for d in galaxies.values()])

# 2. Linear Regression & R-Squared
a, b = np.polyfit(dist_mpc, speed_kms, 1)
r_squared = np.corrcoef(dist_mpc, speed_kms)[0,1]**2

# 3. NASA Extrapolation (2500 km/s)
speed_nasa = 2500
dist_nasa = (speed_nasa - b) / a

# 4. Unit Conversions (Using min/max distances)
d_min, d_max = dist_mpc.min(), dist_mpc.max()
print(f"Distance Range: {d_min*ONE_MPC_LY:.2f} to {d_max*ONE_MPC_LY:.2f} Million ly")
print(f"Note: There are 3 galaxies at the max distance of {d_max} mpc.")

# 5. Plotting
plt.figure(figsize=(10, 6))

# Plot the 3 overlapping points and the others
plt.scatter(dist_mpc, speed_kms, color='red', label='Galaxy Data', zorder=3)

# Regression Line (Extended)
x_fit = np.linspace(0, dist_nasa + 1, 100)
plt.plot(x_fit, a*x_fit + b, color='blue', alpha=0.8, label=f'Fit: $R^2$={r_squared:.2f}')

# NASA Vertical Line & Annotation
plt.plot([dist_nasa, dist_nasa], [0, speed_nasa], 'g--', alpha=0.5)
plt.plot([0, dist_nasa], [speed_nasa, speed_nasa], 'g--', alpha=0.5)
plt.annotate(f'NASA Target: {dist_nasa:.2f} mpc', (dist_nasa, speed_nasa), 
             textcoords="offset points", xytext=(-15, 10), ha='center', color='green')

# Scale and Labels
plt.xlim(0, dist_nasa + 1)
plt.ylim(0, speed_nasa + 500)
plt.xlabel('Distance (mpc)')
plt.ylabel('Speed (km/s)')
plt.title('Hubble Law Regression with NASA Extrapolation')
plt.legend()
plt.grid(True, linestyle=':', alpha=0.6)

plt.show()
Hope yoy will like the effort.

No comments:

Post a Comment

Visualizing Particle Kinematics with Python

Understanding the relationship between position, velocity, and acceleration is fundamental to physics. However, seeing how these variables e...