Plot 3D Surface Heat Equation

asked 2016-11-01 03:02:21 +0100

justinus gravatar image

updated 2016-11-02 06:28:40 +0100

I am trying to plot a 3D surface using SageMath Cloud but I am having some trouble with my plot result. Anyways the program I have written is to plot the Heat Equation solution that I got from analytical method.

The case is: Heat Equation

Here is my code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from sympy import *
from math import *

L = 10
x = np.linspace(0, 10, 5)
t = np.linspace(0, 20, 5)

n = symbols('n', integer=True)
X, T = np.meshgrid(x, t)

Z = []
y = symbols('y')
for ix, ea in enumerate(x):
    ans = 0.
    for n in range(L + 1): # do summation with simple for-loop
        f_pi = 0.
        if x[ix] >= 6 and x[ix] <= 8:
            f_pi = 50
        ans = ans + ((2 / 10.) * integrate(f_pi * sin(radians((2. * n + 1.) * pi * x[ix] / 20.)), (y, 0, 10)) * e**(-1 * (2. * n + 1. / 20.)**2*pi**2*t[ix]) * sin(radians((2. * n + 1.) * pi * x[ix] / 20)))
    Z.append(ans)
Z = np.array(Z, dtype=float)

fig = plt.figure()
ax = fig.gca(projection = '3d')

plt.xticks(x, x)
plt.yticks(t, t)

surf = ax.plot_surface(X, T, Z,
                  rstride = 3,
                  cstride = 3,
                  cmap = cm.coolwarm,
                  linewidth = 0.5,
                  antialiased = True)

fig.colorbar(surf,
         shrink=0.8,
         aspect=16,
         orientation = 'vertical')

ax.view_init(elev=60, azim=60)
ax.dist=8
plt.show()

The plot I got: https://s11.postimg.org/6lk5xrj2r/fig...
It should be: https://s22.postimg.org/4kzl6ff1d/ima... (expected result, using Matlab)

What's wrong with my plotting?

Thanks in advance

edit retag flag offensive close merge delete

Comments

Could you re write the code please.

Sha gravatar imageSha ( 2016-11-01 10:24:17 +0100 )edit

The question has been updated. Sorry for my bad. Hope you can help :)

justinus gravatar imagejustinus ( 2016-11-02 06:24:31 +0100 )edit