I have written a code for heat conductivity of plate. The loop works very strangely counting only the first and last three elements. Can anyone help me?
timend = 8
H = 0.10
n, m = 100, 100
h = H / (n-1)
tau = timend / m
t0, th, ts = 550, 700, 350
rho, c, lam = 8800, 381, 384
x = [[350 for _ in range(n)] for _ in range(m)]
x[0][0] = t0
x[0][n-1] = th
for i in range(1, m):
x[i][0], x[i][n-1] = t0, th
for j in range(1, n-1):
x[i][j] = x[i][j] tau * lam / rho / c * (x[i-1][j 1] - 2 * x[i-1][j] x[i-1][j-1])
print(x[m-1])
CodePudding user response:
I'm interested to learn about this problem and understand what you're trying to implement!
TL;DR -- check your formula inside your nested loop.
Check your code again, and manually step through code inside your loop once or twice, using your numbers:
Given
tau = 0.08
lam = 384
rho = 8800
c = 381
then your code says that
x[i][j] = x[i][j] tau * lam / rho / c * (x[i-1][j 1] - 2 * x[i-1][j] x[i-1][j-1])
which we can equivalently rearrange for simplicity as
x[i][j] = x[i][j] (tau / rho) * (lam / c) * (quantity)
where quantity
is (x[i-1][j-1] x[i-1][j 1] - 2*x[i-1][j])
. We can plug in our numbers:
x[i][j] = x[i][j] (9.09e-6) * (1.008) * (quantity)
If you think about it, when most of your values in the 2D array are 350
, and you're adding 9.6125e-6 * (a quantity approximately 0 in most cases)
, most of your values will remain (to the precision of a float
-type variable) at 350
.