Professional Documents
Culture Documents
1 D Heat
1 D Heat
Parameters:
L (float): Length of the rod.
T (float): Total simulation time.
alpha (float): Thermal diffusivity.
Nx (int): Number of spatial grid points.
Nt (int): Number of time steps.
Returns:
u (2D array): Temperature distribution over space and time.
x (1D array): Spatial coordinates.
t (1D array): Time values.
"""
dx = L / (Nx - 1)
dt = T / Nt
r = alpha * dt / dx**2
if r > 0.5:
print("Warning: Unstable solution. Consider using a smaller time step.")
x = np.linspace(0, L, Nx)
t = np.linspace(0, T, Nt)
u = np.zeros((Nx, Nt))
u[:, 0] = np.sin(np.pi * x) # Initial condition
return u, x, t
# Parameters
L = 1.0 # Length of the rod
T = 0.1 # Total simulation time
alpha = 0.01 # Thermal diffusivity
Nx = 50 # Number of spatial grid points
Nt = 100 # Number of time steps