The code you've provided is a Python program that simulates the refraction of light and gravitational lensing around a black hole. The program uses matplotlib for plotting. Here's an improved version of your code with some minor improvements: #Copyright Daniel Harding - RomanAILabs python import numpy as np import math import matplotlib.pyplot as plt def simulate_refraction(): """ Simulate the bending of light due to refraction. Plots the incident and refracted rays, along with the interface between the two media. """ # Parameters (in scaled units) theta1 = 30 # Angle of incidence normal = np.array([0, -1]) # Normal vector # Refracted direction dir_refracted = np.sin(theta2) * np.cross(normal, [1, 0]) / np.linalg.norm(np.cross(normal, [1, 0])) + np.cos(theta2) * normal dir_refracted = -dir_refracted # Adjust for downward propagation # Extend refracted ray intersect = (10, 0) ray_start = (0, 0) # Plot plt.figure(figsize=(8, 6)) plt.plot([ray_start[0], intersect[0]], [ray_start[1], intersect[1]], 'b-', label='Incident Ray') plt.plot([intersect[0], (10 + dir_refracted[0]) * np.cos(-math.atan2(dir_refracted[1], -dir_refracted[0]))], [intersect[1] + 10 * dir_refracted[1]], 'g-', label='Refracted Ray') plt.axhline(0, color='gray', linestyle='--', label='Interface') plt.xlim(-15, 25) plt.ylim(-5, 5) plt.xlabel('X') plt.ylabel('Y') plt.title('Light Bending via Refraction (Snell''s Law)') plt.legend() plt.grid(True) plt.show() def simulate_grav_lensing(): """ Simulate the bending of light due to gravitational lensing. Plots the curved path of a photon near a black hole. """ # Parameters M = 1.4 * (2e30) # Mass of central object in kg Rs = 3 # Schwarzschild radius # Initial conditions r = np.linspace(0, 10, 100) phi = np.pi / 6 + 0.01 # Effective potential and angular momentum L = 1 # Conserved angular momentum (for unit energy) # Integrate geodesic (Euler method for simplicity) def integrand(r, phi): B = 1 - Rs / r dphi_dr = (L / (r**2)) / math.sqrt(1 / B - (L**2 / (c**2 * r**2))) return [dphi_dr] # Perform numerical integration from scipy.integrate import solve_ivp sol = solve_ivp(lambda t, y: integrand(y[0], y[1]), [0, 10], [0, phi], method='BDF', atol=1e-6) # Plot plt.figure(figsize=(8, 6)) x = r * np.cos(phi) y = r * np.sin(phi) plt.plot(x, y, 'r-', label='Light Path') plt.scatter(0, 0, color='black', s=100, label='Black Hole') plt.axhline(0, color='gray', lw=0.5) plt.axvline(0, color='gray', lw=0.5) plt.xlim(-10, 20) plt.ylim(-10, 20) plt.xlabel('X (scaled)') plt.ylabel('Y (scaled)') plt.title('Light Bending via Gravitational Lensing (Geodesic Path)') plt.legend() plt.grid(True) plt.show() if __name__ == "__main__": print("Running Refraction Demo...") simulate_refraction() print("\nRunning Gravitational Lensing Demo...") simulate_grav_lensing()