# 1D heat transfer

**URL:** <https://discuss.python.org/t/1d-heat-transfer/16734>\
**Category:** Python Help\
**Tags:** help\
**Created:** [June 22, 2022, 7:14am UTC](https://discuss.python.org/t/1d-heat-transfer/16734 "2022-06-22T07:14:09Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![root-11](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/root-11/32/2191_2.png) [@root-11](https://discuss.python.org/u/root-11)\
**Post date:** [June 22, 2022, 9:02am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/3 "2022-06-22T09:02:42Z")

</div>

For a start I think you’ve missed declaring `dx2` somewhere in your code. Check out my comment below:

```auto
import numpy as np
import matplotlib.pyplot as plt

L = 10
n = 10

T0= 0
T_rock = 600
T_magma = 1300

alpha = 10**(-6)

dx = L/n

t_final = 60
dt = 0.1

x = np.linspace(dx/2, L-dx/2, n)
t = np.arange(0, t_final, dt)

dTdt = np.empty(n)

T = np.ones(n)*T0

for j in range(1, len(t)):
    for i in range(1, n-1):
        dTdt[i] = alpha*(-(T[i]-T[i-1])/dx2 + (T[i+1]-T[i])/dx2) # <---- where did dx2 come from?
        dTdt[0] = alpha*(-(T0-T_magma)/dx2 + (T[1]-T0)/dx2)
        dTdt[n-1] = alpha*(-(T[n-1]-T[n-2])/dx2 + (T_rock-T[n-1])/dx2)
        T = T + dTdt*dt

plt.figure(1)
plt.plot(x,T)
plt.axis([0, L , 500, 1500])
plt.xlabel('Distance [m]')
plt.ylabel('Temperature [°C]')
plt.pause(0.05)
plt.show()

```

Please post your traceback in [tripple back](https://www.markdownguide.org/basic-syntax/#code) tics for any subsequent errors.  
They’re really helpful for us trying to help you 🙂

---

_[View the full topic](https://discuss.python.org/t/1d-heat-transfer/16734)._
