I am following this lesson 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).
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: