-
Notifications
You must be signed in to change notification settings - Fork 0
/
bvp.py
44 lines (36 loc) · 1022 Bytes
/
bvp.py
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
import numpy as np
import matplotlib.pyplot as plt
# Constants and Parameters
alpha = 0.01
L = 10
T = 2
Nx = 100 # Number of spatial points
Nt = 500 # Number of time points
dx = L / (Nx - 1)
dt = T / (Nt - 1)
# Function for T_0(t)
def T_0(t):
return 100 * np.sin(np.pi * t / T)
# Initial condition f(x)
def f(x):
return 20 # Constant initial temperature
# Create grid
x = np.linspace(0, L, Nx)
t = np.linspace(0, T, Nt)
u = np.zeros((Nx, Nt))
# Set initial condition
u[:, 0] = f(x)
# Finite difference method
for n in range(0, Nt - 1):
for i in range(1, Nx - 1):
u[i, n + 1] = u[i, n] + alpha * dt / dx**2 * (u[i + 1, n] - 2 * u[i, n] + u[i - 1, n])
# Boundary conditions
u[0, n + 1] = T_0(t[n + 1])
u[-1, n + 1] = 50 # T_L
# Plotting the results
plt.imshow(u, extent=[0, T, 0, L], origin='lower', aspect='auto')
plt.colorbar(label='Temperature')
plt.xlabel('Time')
plt.ylabel('Position along rod')
plt.title('Heat Equation with Non-Uniform Boundary Conditions')
plt.show()