Numerical Code - OneD SS Q FDM
Numerical Code - OneD SS Q FDM
Boundary Conditions
● Temperature at the left boundary T ( 0 )=0 ° C
Rod Properties
● Length L=0.1 m
● Node 2: x 2=0.025 m
● Node 3: x 3=0.05 m
● Node 4: x 4 =0.075 m
d T T i +1−2T i +T i−1
2
2
≈
dx ( Δx )2
Simplify:
2
−Q⋅ ( Δx )
T i+1−2 T i+T i−1=
k
● T 1=0 ° C
● T 5=100 ° C
Using the finite difference approximation, write equations for internal nodes:
Node 2 (i=2):
2
−Q⋅ ( Δx )
T 3−2 T 2 +T 1=
k
Substitute T 1=0:
2
−Q ⋅ ( Δx )
T 3−2 T 2=
k
Node 3 (i=3):
2
−Q ⋅ ( Δx )
T 4−2 T 3+ T 2=
k
Node 4 (i=4):
2
−Q ⋅ ( Δx )
T 5−2 T 4+ T 3=
k
Substitute T 5=100:
2
−Q ⋅ ( Δx )
100−2T 4 +T 3=
k
3. Matrix Formulation
Express the equations for internal nodes in matrix form:
[ ]
2 2 2
−Q ⋅ ( Δx ) Q⋅ ( Δx ) Q ⋅ ( Δx )
[ −2 10 1−21 0 1−2 ] [ T 2 T 3 T 4 ]= − −100−
k k k
Let:
[ ]
2 2 2
−Q ⋅ ( Δx ) Q⋅ ( Δx ) Q ⋅ ( Δx )
A=[−21 0 1−2 1 01−2 ] ,T =[ T 2 T 3 T 4 ] , B= − −100−
k k k
Inverse of Matrix A :
−1
A =[ −0.75−0.5−0.25−0.5−1−0.5−0.25−0.5−0.75 ]
Solution for T :
Substitute values:
● Δx=0.025 m
● k =401 W /mK
4 3
● Q=5 × 10 W /m
2 4 2
Q⋅ ( Δx ) 5 ×10 ⋅ ( 0.025 )
= =0.078 ° C
k 401
B=[ −0.078−0.078−100−0.078 ] =[ −0.078−0.078−100.078 ]
Solve for T :
T =[ 24.96 49.92 74.88 ]
Thus:
T 2=24.96 °C ,T 3=49.92° C ,T 4=74.88° C
5. Analytical Solution
2
d T Q
The general solution of 2
+ =0 is:
dx k
−Q 2
T ( x )= x +C 1 x +C2
2k
Using boundary conditions:
● At x=0 , T =0 : C 2=0
● At x=0.1 , T =100:
4
−5 ×10 ( )2
100= 0.1 +C 1 ⋅0.1
2⋅ 401
Solve for C1C_1:
100=−6.23+0.1 C 1 ⟹C 1=1062.3
Thus:
4
−5 ×10 2
T ( x )= x + 1062.3 x
2 ⋅ 401
Temperature at Nodes:
● T 1=T ( 0 )=0 ° C
6. Comparison of Solutions
Nod Numerical Solution (T Analytical Solution (T
e ) )
1 0 0
2 24.96 24.96
3 49.92 49.92
4 74.88 74.88
5 100 100
7. Conclusion
The numerical and analytical solutions match perfectly, even with uniform heat generation,
confirming the accuracy of the finite difference method for this problem.
8 . Code
import numpy as np
import matplotlib.pyplot as plt
# Parameters
L = 0.1 # Length of the rod (m)
k = 401 # Thermal conductivity (W/m·K)
Q = [5e7, 8e7, 5e4] # Heat generation rate (W/m^3)
T_left = 0 # Dirichlet boundary condition at x=0 (°C)
T_right = 100 # Dirichlet boundary condition at x=L (°C)
N = 25 # Number of nodes (including boundary nodes)
# Discretization
dx = L / (N - 1)
x = np.linspace(0, L, N)
# Plotting
plt.figure(figsize=(8, 8))
for i, q in enumerate(Q):
# Coefficient matrix A and right-hand side vector B
A = np.zeros((N, N))
B = np.zeros(N)
for j in range(1, N - 1):
A[j, j - 1] = 1
A[j, j] = -2
A[j, j + 1] = 1
B[j] = -q * dx**2 / k
# Analytical solution
C1 = (T_right - T_left + (q / (2 * k)) * L**2) / L
C2 = T_left
T_analytical = -(q / (2 * k)) * x**2 + C1 * x + C2
plt.xlabel("Position (m)")
plt.ylabel("Temperature (°C)")
plt.title("Temperature Distribution in a Rod with Uniform Heat Generation")
plt.legend()
plt.grid()
plt.show()