我正在尝试使用SageMath云绘制一个3D曲面,但我遇到了一些问题,因为matplotlib的文档看起来不太透彻,而且缺乏示例。无论如何,我编写的程序是绘制我从分析方法得到的热方程解。
案例是:热方程
这是我的代码:
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 *
x = np.linspace(-8, 8, 100)
t = np.linspace(-8, 8, 100)
n = symbols('n', integer=True)
X, T = np.meshgrid(x, t)
an = float(2 / 10) * integrate(50 * sin(radians((2 * n + 1) * pi * x / 20)), (x, 0, 10))
Z = summation(an * e**(2 * n + 1 / 20)**2*pi**2*t * sin(radians((2 * n + 1) * pi * x / 20)), (n, 0, oo))
fig = plt.figure()
ax = fig.gca(projection = '3d')
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=50)
ax.dist=8
plt.show()当我运行代码来绘制图形时,我会得到这样的错误:“第7-7行回溯(最近一次调用)中的错误:文件"/projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_server.py",第968行,在执行exec编译中(块+‘\n’,'',‘单’)在命名空间中,局部变量文件”,第1行,在文件"/projects/sage/sage-7.3/local/lib/python2.7/site-packages/numpy/core/function_base.py",第93行中,在linspace dt =result_type(开始、停止、浮动(Num)) TypeError:数据类型未被理解“
请,任何和所有的帮助都是非常感谢的。我认为出现错误是因为我定义了x = np.linspace(-8, 8, 100)和t = np.linspace(-8, 8, 100),但是有必要让原始程序运行。我不知道如何纠正这个问题,所以这个图表画得很好。谢谢!
发布于 2016-11-01 05:31:40
这两行代码都有问题:
an = float(2 / 10) * integrate(50 * sin(radians((2 * n + 1) * pi * x / 20)), (x, 0, 10))
Z = summation(an * e**(2 * n + 1 / 20)**2*pi**2*t * sin(radians((2 * n + 1) * pi * x / 20)), (n, 0, oo))我建议您使用简单的for -循环来计算Z的其他一些简单值,以确认每件事情都很好。尝试将这2行替换为:
# begin simple calculation for Z
# offered just for example
Z = []
y = symbols('y') # declare symbol for integration
for ix,ea in enumerate(x):
ans = integrate(y * sin(ea / 20), (y, 0, x[ix])) # integrate y from y=0 to y=x(ix)
Z.append(ans)
Z = np.array(Z, dtype=float) # convert Z to array
# end of simple calculation for Z当你运行它,你应该得到一些阴谋作为结果。对于您预期的Z值,您有更好的情况来使用简单的For -循环来计算它们。

https://stackoverflow.com/questions/40352692
复制相似问题