Professional Documents
Culture Documents
1
1
discretization:
First, let's define the problem we want to solve. We want to find the temperature distribution in a
thin rod of length L, with an initial temperature distribution T(x,0) = f(x) and boundary conditions
T(0,t) = T(L,t) = 0. The diffusion equation that describes this problem is:
∂T/∂t = α ∂²T/∂x²
We can discretize this equation using the finite difference method. Let's use the forward difference
for the time derivative and the centered difference for the space derivative. Let's also assume that
the time step Δt and the space step Δx are constant. Then, the discretized equation is:
Now, let's implement this equation in Python. We will use the numpy library to create arrays for the
temperature distribution and to perform the calculations.
import numpy as np
T = T0.copy()
# Time loop
t = 0.0
T[0] = 1.0
T[N-1] = 0.0
t = t + dt
plt.plot(x, T)
plt.xlabel('x')
plt.ylabel('Temperature')
plt.show()
In this code, we first define the parameters of the problem, including the length of the rod, the
number of grid points, the thermal diffusivity, the initial temperature distribution, the maximum
simulation time, and the time step. We then create the grid and initialize the temperature
distribution. We use a while loop to update the temperature distribution at each time step using the
discretized diffusion equation. We apply the boundary conditions at each time step by setting the
temperature at the left and right boundaries to their prescribed values. Finally, we plot the
temperature distribution as a function of position.
This code can be used to solve the one-dimensional diffusion equation for different initial
temperature distributions and boundary conditions by changing the parameters in the code. The key
to discretizing