# 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:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Castro.95](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@Castro.95](https://discuss.python.org/u/Castro.95)\
**Post date:** [June 22, 2022, 7:14am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/1 "2022-06-22T07:14:09Z")

</div>

Hi guys, I am in this forum and basically new at numerical modelling.  
I am trying to solve a transient 1D heat transfer equation following a youtube tutorial and adapting it to my data. Even if there seem not to be any error, the code does not plot anything.  
Can you help me to find the problem? Thank you a lot 🙂

Here is my code:

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])/dx**2 + (T[i+1]-T[i])/dx**2)  
dTdt[0] = alpha\*(-(T0-T\_magma)/dx**2 + (T[1]-T0)/dx**2)  
dTdt[n-1] = alpha\*(-(T[n-1]-T[n-2])/dx**2 + (T\_rock-T[n-1])/dx**2)

```
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()

---

<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 🙂

---

<div class="post-metadata">

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

</div>

> [@root-11](#):
>
> I think you’ve missed declaring `dx2`

I think in the original code it was `dx**2`. Markdown really messes the code up. I think that the forum should have some autodetection of (Python) code not between triple backticks. Almost all the new users here do not understand the problem with formatting.

---

<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:24am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/5 "2022-06-22T09:24:57Z")

</div>

I see it now. The ` **` followed by `** ` turned the mid section into **bold** instead.

---

<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:31am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/6 "2022-06-22T09:31:53Z")

</div>

There’s nothing wrong with the code. I’m running python3.9 on windows as this:

```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])/dx**2 + (T[i+1]-T[i])/dx**2)
        dTdt[0] = alpha*(-(T0-T_magma)/dx**2 + (T[1]-T0)/dx**2)
        dTdt[n-1] = alpha*(-(T[n-1]-T[n-2])/dx**2 + (T_rock-T[n-1])/dx**2)
        T = T + dTdt*dt

```

the issue is in the plotting:

```auto
plt.figure()
plt.xlabel('Distance [m]')
plt.ylabel('Temperature [°C]')
# plt.axis([0, L , 500, 1500]) # <--- disabled.
plt.plot(x,T)
# plt.pause(0.05) # <--- disabled.
plt.show()

```

where I get this after disabling two rows.

![output](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/e/eefaa6c3684da2f7cd3e6976bc61790689631155.png)

---

<div class="post-metadata">

**Author:** ![Castro.95](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@Castro.95](https://discuss.python.org/u/Castro.95)\
**Post date:** [June 22, 2022, 11:34am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/7 "2022-06-22T11:34:43Z")

</div>

Yes, that a was a naif error. I have found out where the problem was and, beyond the correction of dx\*\*2, also parameter setting was lacking of physical meanng. Thank you very much for your support and suggestions! 🙂

---

<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, 11:52am UTC](https://discuss.python.org/t/1d-heat-transfer/16734/8 "2022-06-22T11:52:48Z")

</div>

You’re welcome 😉
