Search code examples
pythonpython-3.xplotgraph3d

Two-dimensional heat equation in 3d graph - python


I have a problem, i.e. I don't know how to draw a 3D graph in python according to the given equation that I calculated. The graph is limited to x (from 0 to 1) and to y (from 0 to 2). n is a variable that goes from 1 to infinity. the sum of all solutions must be plotted on the 3d graph. I am attaching a picture of the equation and what the graph should look like. what graph should look like

equation

Can you help me please?

I tried to create this graph in python, geogebra, mathlab,... but i don't know how. I need this for the project assignment and I only have two days more.


Solution

  • import numpy as np
    import matplotlib.pyplot as plt
    
    # Heat equation at coordinates x and y
    # Will add up first n terms of summation
    def heat_eq(x, y, n):
        total = 0
    
        for i in range(1, n + 1):
            total += (
                np.sin(i * np.pi * x)
                * -8
                / (i**3 * np.pi**3 * np.sinh(2 * i * np.pi))
                * np.sin((2 - y) * i * np.pi)
            )
    
        return total
    
    
    # 1-D arrays of x and y coordinates
    x = np.arange(0, 1, 0.1)
    y = np.arange(0, 2, 0.1)
    
    # Turn x,y into 2D coordinates
    y, x = np.meshgrid(y, x, indexing="ij")
    
    # Get heat equation values at x,y coordinates. n = 100 seems sufficient for convergence
    z = heat_eq(x, y, 100)
    
    # Basic 3D plotting
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(projection="3d")
    ax.plot_surface(x, y, z, linewidth=0, antialiased=True, cmap="coolwarm")
    

    Plot