courses:ast401:4.3
Differences
This shows you the differences between two versions of the page.
| courses:ast401:4.3 [2025/07/21 02:12] – created asad | courses:ast401:4.3 [2025/07/21 02:31] (current) – [Python Code Example] asad | ||
|---|---|---|---|
| Line 65: | Line 65: | ||
| - **Step 7**: Repeat inward to the core. | - **Step 7**: Repeat inward to the core. | ||
| - | ==== Python | + | ==== Python ==== |
| - | + | < | |
| - | Here's a Python implementation of the method: | + | |
| - | + | ||
| - | ```python | + | |
| - | import numpy as np | + | |
| - | import matplotlib.pyplot as plt | + | |
| - | + | ||
| - | # Constants | + | |
| - | G = 6.67430e-11 | + | |
| - | R_earth = 6.371e6 | + | |
| - | rho_surface = 3000 | + | |
| - | M_earth = 5.972e24 | + | |
| - | N = 100 | + | |
| - | dr = R_earth | + | |
| - | + | ||
| - | # Seismic speeds | + | |
| - | vp = 8000 # m/s | + | |
| - | vs = 4500 # m/s | + | |
| - | K_over_rho = vp**2 - (4/3) * vs**2 | + | |
| - | + | ||
| - | # Initialize | + | |
| - | r = np.linspace(R_earth, | + | |
| - | rho = np.zeros(N) | + | |
| - | mass = np.zeros(N) | + | |
| - | rho[0] = rho_surface | + | |
| - | mass[0] = M_earth | + | |
| - | + | ||
| - | # Loop | + | |
| - | for i in range(1, N): | + | |
| - | mass[i] = mass[i-1] - 4*np.pi*r[i-1]**2 * dr * rho[i-1] | + | |
| - | delta_rho = - (rho[i-1]**2 * G * mass[i]) | + | |
| - | rho[i] = rho[i-1] + delta_rho | + | |
| - | + | ||
| - | # Plot | + | |
| - | plt.plot(r | + | |
| - | plt.xlabel("Radius (km)" | + | |
| - | plt.ylabel(" | + | |
| - | plt.title(" | + | |
| - | plt.gca().invert_xaxis() | + | |
| - | plt.show() | + | |
courses/ast401/4.3.1753085538.txt.gz · Last modified: by asad
