1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.optimize import minimize_scalar
A = np.array([0, 10]) B = np.array([20, 0]) interface_y = 5 v1 = 5.0 v2 = 2.0
def travel_time(x, A, B, interface_y, v1, v2): """ Calculate total travel time as a function of the x-coordinate where the path crosses the interface. Parameters: x: x-coordinate at the interface A: starting point [x, y] B: ending point [x, y] interface_y: y-coordinate of the interface v1: speed in medium 1 (above interface) v2: speed in medium 2 (below interface) Returns: Total travel time """ d1 = np.sqrt((x - A[0])**2 + (interface_y - A[1])**2) d2 = np.sqrt((B[0] - x)**2 + (B[1] - interface_y)**2) time = d1 / v1 + d2 / v2 return time
result = minimize_scalar( lambda x: travel_time(x, A, B, interface_y, v1, v2), bounds=(0, 20), method='bounded' )
optimal_x = result.x optimal_time = result.fun
print(f"Optimal crossing point: x = {optimal_x:.4f} m") print(f"Minimum travel time: {optimal_time:.4f} s")
interface_point = np.array([optimal_x, interface_y]) theta1 = np.arctan(np.abs(optimal_x - A[0]) / np.abs(interface_y - A[1])) theta2 = np.arctan(np.abs(B[0] - optimal_x) / np.abs(B[1] - interface_y))
print(f"\nAngle in medium 1 (from normal): {np.degrees(theta1):.4f}°") print(f"Angle in medium 2 (from normal): {np.degrees(theta2):.4f}°") print(f"\nSnell's Law verification:") print(f"n1 * sin(θ1) = {(1/v1) * np.sin(theta1):.6f}") print(f"n2 * sin(θ2) = {(1/v2) * np.sin(theta2):.6f}") print(f"Ratio v1/v2: {v1/v2:.4f}") print(f"Ratio sin(θ1)/sin(θ2): {np.sin(theta1)/np.sin(theta2):.4f}")
fig = plt.figure(figsize=(18, 12))
ax1 = plt.subplot(2, 3, 1) x_range = np.linspace(0, 20, 1000) times = [travel_time(x, A, B, interface_y, v1, v2) for x in x_range]
ax1.plot(x_range, times, 'b-', linewidth=2, label='Travel time') ax1.axvline(optimal_x, color='r', linestyle='--', linewidth=2, label=f'Optimal x = {optimal_x:.2f}m') ax1.axhline(optimal_time, color='g', linestyle='--', linewidth=1, alpha=0.5, label=f'Min time = {optimal_time:.2f}s') ax1.scatter([optimal_x], [optimal_time], color='r', s=100, zorder=5) ax1.set_xlabel('Crossing Point x (m)', fontsize=12) ax1.set_ylabel('Travel Time (s)', fontsize=12) ax1.set_title('Travel Time vs Crossing Point', fontsize=14, fontweight='bold') ax1.grid(True, alpha=0.3) ax1.legend(fontsize=10)
ax2 = plt.subplot(2, 3, 2)
ax2.fill_between([0, 20], interface_y, 15, color='#FFE5B4', alpha=0.5, label='Sand (faster)') ax2.fill_between([0, 20], 0, interface_y, color='#87CEEB', alpha=0.5, label='Water (slower)')
ax2.plot([A[0], optimal_x], [A[1], interface_y], 'r-', linewidth=3, label='Optimal path') ax2.plot([optimal_x, B[0]], [interface_y, B[1]], 'r-', linewidth=3)
ax2.plot([A[0], B[0]], [A[1], B[1]], 'b--', linewidth=2, alpha=0.6, label='Straight line')
ax2.scatter(*A, color='green', s=200, zorder=5, marker='o', edgecolors='black', linewidths=2) ax2.scatter(*B, color='red', s=200, zorder=5, marker='s', edgecolors='black', linewidths=2) ax2.scatter(optimal_x, interface_y, color='orange', s=150, zorder=5, marker='D', edgecolors='black', linewidths=2)
ax2.text(A[0]-1, A[1]+0.5, 'A (Start)', fontsize=11, fontweight='bold') ax2.text(B[0]+0.5, B[1]-0.5, 'B (End)', fontsize=11, fontweight='bold') ax2.text(optimal_x+0.5, interface_y+0.5, f'({optimal_x:.1f}, {interface_y})', fontsize=10)
ax2.axhline(interface_y, color='black', linestyle='-', linewidth=2, alpha=0.7) ax2.set_xlabel('x (m)', fontsize=12) ax2.set_ylabel('y (m)', fontsize=12) ax2.set_title('Optimal Path Through Two Media', fontsize=14, fontweight='bold') ax2.legend(fontsize=10, loc='upper right') ax2.grid(True, alpha=0.3) ax2.set_aspect('equal')
ax3 = plt.subplot(2, 3, 3) test_x_values = np.linspace(2, 18, 8) colors = plt.cm.viridis(np.linspace(0, 1, len(test_x_values)))
for i, test_x in enumerate(test_x_values): time = travel_time(test_x, A, B, interface_y, v1, v2) ax3.plot([A[0], test_x, B[0]], [A[1], interface_y, B[1]], color=colors[i], alpha=0.6, linewidth=2, label=f'x={test_x:.1f}m, t={time:.2f}s')
ax3.plot([A[0], optimal_x, B[0]], [A[1], interface_y, B[1]], 'r-', linewidth=4, label=f'Optimal: x={optimal_x:.1f}m, t={optimal_time:.2f}s')
ax3.fill_between([0, 20], interface_y, 15, color='#FFE5B4', alpha=0.3) ax3.fill_between([0, 20], 0, interface_y, color='#87CEEB', alpha=0.3) ax3.axhline(interface_y, color='black', linestyle='-', linewidth=2, alpha=0.7) ax3.scatter(*A, color='green', s=150, zorder=5, marker='o', edgecolors='black', linewidths=2) ax3.scatter(*B, color='red', s=150, zorder=5, marker='s', edgecolors='black', linewidths=2)
ax3.set_xlabel('x (m)', fontsize=12) ax3.set_ylabel('y (m)', fontsize=12) ax3.set_title('Comparison of Different Paths', fontsize=14, fontweight='bold') ax3.legend(fontsize=8, loc='upper right') ax3.grid(True, alpha=0.3) ax3.set_aspect('equal')
ax4 = fig.add_subplot(2, 3, 4, projection='3d')
x_cross = np.linspace(0, 20, 100) y_start = np.linspace(6, 14, 100) X, Y = np.meshgrid(x_cross, y_start) Z = np.zeros_like(X)
for i in range(len(y_start)): for j in range(len(x_cross)): A_temp = np.array([0, y_start[i]]) Z[i, j] = travel_time(x_cross[j], A_temp, B, interface_y, v1, v2)
surf = ax4.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8, edgecolor='none') ax4.scatter([optimal_x], [A[1]], [optimal_time], color='red', s=100, marker='o')
ax4.set_xlabel('Crossing Point x (m)', fontsize=10) ax4.set_ylabel('Starting Height y (m)', fontsize=10) ax4.set_zlabel('Travel Time (s)', fontsize=10) ax4.set_title('3D Travel Time Surface', fontsize=14, fontweight='bold') fig.colorbar(surf, ax=ax4, shrink=0.5, aspect=5)
ax5 = plt.subplot(2, 3, 5) x_fine = np.linspace(0.5, 19.5, 1000) times_fine = np.array([travel_time(x, A, B, interface_y, v1, v2) for x in x_fine]) derivative = np.gradient(times_fine, x_fine)
ax5.plot(x_fine, derivative, 'b-', linewidth=2, label='dT/dx') ax5.axhline(0, color='black', linestyle='-', linewidth=1, alpha=0.5) ax5.axvline(optimal_x, color='r', linestyle='--', linewidth=2, label=f'Optimal x = {optimal_x:.2f}m') ax5.scatter([optimal_x], [0], color='r', s=100, zorder=5) ax5.set_xlabel('Crossing Point x (m)', fontsize=12) ax5.set_ylabel('dT/dx (s/m)', fontsize=12) ax5.set_title('Derivative of Travel Time (Zero at Minimum)', fontsize=14, fontweight='bold') ax5.grid(True, alpha=0.3) ax5.legend(fontsize=10)
ax6 = plt.subplot(2, 3, 6)
ax6.fill_between([0, 8], interface_y, 10, color='#FFE5B4', alpha=0.5) ax6.fill_between([0, 8], 0, interface_y, color='#87CEEB', alpha=0.5) ax6.axhline(interface_y, color='black', linestyle='-', linewidth=2)
scale_factor = 0.3 A_scaled = np.array([2, interface_y + 3]) B_scaled = np.array([6, interface_y - 3]) interface_point_scaled = np.array([optimal_x * scale_factor + 2, interface_y])
ax6.plot([A_scaled[0], interface_point_scaled[0]], [A_scaled[1], interface_point_scaled[1]], 'r-', linewidth=3) ax6.plot([interface_point_scaled[0], B_scaled[0]], [interface_point_scaled[1], B_scaled[1]], 'r-', linewidth=3)
ax6.plot([interface_point_scaled[0], interface_point_scaled[0]], [2, 8], 'g--', linewidth=2, label='Normal')
from matplotlib.patches import Arc arc1 = Arc(interface_point_scaled, 1.5, 1.5, angle=0, theta1=90-np.degrees(theta1), theta2=90, color='blue', linewidth=2) arc2 = Arc(interface_point_scaled, 1.5, 1.5, angle=0, theta1=270, theta2=270+np.degrees(theta2), color='orange', linewidth=2) ax6.add_patch(arc1) ax6.add_patch(arc2)
ax6.scatter(*A_scaled, color='green', s=150, zorder=5, marker='o', edgecolors='black', linewidths=2) ax6.scatter(*B_scaled, color='red', s=150, zorder=5, marker='s', edgecolors='black', linewidths=2)
ax6.text(A_scaled[0]-0.5, A_scaled[1]+0.3, 'A', fontsize=12, fontweight='bold') ax6.text(B_scaled[0]+0.3, B_scaled[1]-0.3, 'B', fontsize=12, fontweight='bold') ax6.text(interface_point_scaled[0]+0.8, interface_y+1.5, f'θ₁={np.degrees(theta1):.1f}°', fontsize=10) ax6.text(interface_point_scaled[0]+0.8, interface_y-1.5, f'θ₂={np.degrees(theta2):.1f}°', fontsize=10) ax6.text(1, 8, f'v₁ = {v1} m/s', fontsize=11, bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5)) ax6.text(1, 1, f'v₂ = {v2} m/s', fontsize=11, bbox=dict(boxstyle='round', facecolor='lightblue', alpha=0.5))
ax6.set_xlabel('x (m)', fontsize=12) ax6.set_ylabel('y (m)', fontsize=12) ax6.set_title("Snell's Law: Angle Visualization", fontsize=14, fontweight='bold') ax6.legend(fontsize=10) ax6.grid(True, alpha=0.3) ax6.set_xlim(0, 8) ax6.set_ylim(0, 10) ax6.set_aspect('equal')
plt.tight_layout() plt.savefig('fermat_principle_analysis.png', dpi=150, bbox_inches='tight') plt.show()
print("\n" + "="*60) print("ANALYSIS COMPLETE") print("="*60)
|