Search code examples
pythonfftdifferential-equations

How to make 3D model of heat equation in Python?


Given:

enter image description here

and

We have formula:

enter image description here

I make 3D model, but I can't give the condition like when x = 0 u(0,t) = 0

import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def u(x,t,n):
    for i in range(1,n):
        alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
        e=np.exp((-(np.pi**2)*(i**2)*t))
        sin=np.sin((i*np.pi*x)/3)
        u=alpha*e*sin
    return u

N=20
L = 4  # length
att = 20 # iteration

x = np.linspace(0, L ,N) #x-array
t = np.linspace(0, L, N) #t-array
X, Y = np.meshgrid(x, t)

Z = u(X, Y, att)

fig = plt.figure(figsize = (10,10))

ax = fig.add_subplot(111, projection='3d')

ax.plot_wireframe(X, Y, Z, rstride=10, cstride=1000)
plt.show()

My 3D model:

enter image description here


Solution

  • It would help if you actually computed a sum in the partial Fourier sum calculation, at the moment you just return the last term of that sum.

    def u(x,t,n):
        u = 0*x
        for i in range(1,n):
            alpha=((6*(-1)**i-30)/(i**2*np.pi**2))
            e=np.exp((-(np.pi**2)*(i**2)*t))
            sin=np.sin((i*np.pi*x)/3)
            u+=alpha*e*sin
        return u
    

    Are you sure about the Fourier coefficients? The number 30 in it is for me somewhat suspicious. Also the frequency seems strange, the continuation of u(x,0) should be an odd rectangular wave of period 8. Notice, it is a=3 but L=4.