I need to solve the heat equation using finite difference method in Python. However my main problem is not the solving equation but rather plotting the result. See below for my code.
import numpy as np
import matplotlib.pyplot as plt
max_iter_time = 100
interval_length = 1
D = 20
dt = .001
delta_x = .01
u = np.empty((max_iter_time,round(interval_length/delta_x)))
interval = np.linspace(0,1,round(interval_length/delta_x))
interior = np.sin(interval*2*np.pi)
u_start = 0
u = np.array([interior]*max_iter_time)
u[:,0]=u_start
for i in range(0, max_iter_time-1,1):
u[:,round(interval_length/delta_x)-1]=u[:,round(interval_length/delta_x)-2]
def fdm1d(u):
for t in range(0, max_iter_time-1, 1):
for i in range(0, max_iter_time-1, 1):
u[t + 1, i] = u[t][i] + D*dt * (u[t][i+1] + u[t][i-1] + u[t][i] - 2*u[t][i])
return u
u2 = []
u1 = fdm1d(u)
print(u1.shape)
ax = plt.axes(projection='3d')
for t in range(0, max_iter_time-1, 1):
for i in range(0, max_iter_time-1, 1):
u2.append([t,i,u1[t][i]])
xpoints = []
ypoints = []
zpoints =[]
for i in range(len(u2)-1):
xpoints.append(u2[i][0])
ypoints.append(u2[i][1])
zpoints.append(u2[i][2])
ax.scatter(xpoints,ypoints,zpoints,cmap='inferno')
plt.show()
My question is about the plot itself - when I compile the code the plot that shows looks like this: plot
In the code I have specified the color of this but nothing has changed. Could someone adivse on this? What do I do wrong here? Is there any better way to plot this?