A

#### Armadillo

##### Guest

Code:

```
import numpy
from matplotlib import pyplot
length = 2
k = .466
temp_left = 200
temp_right = 200
total_time = 4
dx = .1
x_vec = numpy.linspace(0, length, int(length/dx))
dt = .0001
t_vec = numpy.linspace(0, total_time, int(total_time/dt))
u = numpy.zeros([len(t_vec), len(x_vec)])
u[:, 0] = temp_left
u[:, -1] = temp_right
for t in range(1, len(t_vec)-1):
for x in range(1, len(x_vec)-1):
u[t+1, x] = k * (dt / dx**2) * (u[t, x+1] - 2*u[t, x] +
u[t, x-1]) + u[t, x]
pyplot.plot(x_vec, u[t], 'black')
pyplot.pause(.001)
pyplot.cla()
pyplot.plot(x_vec, u[0])
pyplot.ylabel("Temperature (C˚)")
pyplot.xlabel("Distance Along Rod (m)")
pyplot.show()
```

I am just learning to code, and this is all new to me. I am working in a jupyter notebook version 7.1.2. I placed the code into the jupyter notebook cell, ran the cell, and was expecting a single dynamic plot. Instead, what I got was many static plots.

I've looked at the examples found here to see if I can modify the code to work for my application. In trying every example I would get a blank figure, like so:

<p>I am following <a href="https://medium.com/@matiasortizdiez...thon-i-solving-the-heat-equation-bf0ae5d4c37f" rel="nofollow noreferrer">this lesson</a> on solving the heat equation in python. The lesson states that after solving the heat equation, we can visualize the solution by simply calling pyplot.plot() inside the loop for an animated plot, where the code below will dynamically plot the temperature at every point at every time, resulting in an animated plot (an example of the animated plot is provided in the lesson post).</p>

<pre><code>import numpy

from matplotlib import pyplot

length = 2

k = .466

temp_left = 200

temp_right = 200

total_time = 4

dx = .1

x_vec = numpy.linspace(0, length, int(length/dx))

dt = .0001

t_vec = numpy.linspace(0, total_time, int(total_time/dt))

u = numpy.zeros([len(t_vec), len(x_vec)])

u[:, 0] = temp_left

u[:, -1] = temp_right

for t in range(1, len(t_vec)-1):

for x in range(1, len(x_vec)-1):

u[t+1, x] = k * (dt / dx**2) * (u[t, x+1] - 2*u[t, x] +

u[t, x-1]) + u[t, x]

pyplot.plot(x_vec, u[t], 'black')

pyplot.pause(.001)

pyplot.cla()

pyplot.plot(x_vec, u[0])

pyplot.ylabel("Temperature (C˚)")

pyplot.xlabel("Distance Along Rod (m)")

pyplot.show()

</code></pre>

<p>I am just learning to code, and this is all new to me. I am working in a jupyter notebook version 7.1.2. I placed the code into the jupyter notebook cell, ran the cell, and was expecting a single dynamic plot. Instead, what I got was many static plots.</p>

<p>I've looked at the examples <a href="https://pythonguides.com/matplotlib-update-plot-in-loop/" rel="nofollow noreferrer">found here</a> to see if I can modify the code to work for my application. In trying every example I would get a blank figure, like so:

<a href="https://i.sstatic.net/bZglKccU.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bZglKccU.png" alt="Screenshot of jupyter notebook code and resulting blank figure" /></a></p>