我有这样的计算(积分,然后求和)
结果是s
,它是关于自变量x
和t
的。
但是,当我尝试用return s
将其定义为u(x,t)
时,它无法将x
变量替换为我定义为x = np.linspace(0, 20, 50)
的变量,并将t
变量替换为t = np.linspace(0, 10, 50)
。我认为它仍然遵循sympy在开始时定义的x
,t
也是如此。
我想画一个3D图/线框图。Z
轴是u(x,t)
。希望任何人都能在这方面帮助我。
我的MWE:
import numpy as np
import sympy as sm
from sympy import *
#from spb import *
#from mpmath import nsum, inf
x = sm.symbols("x")
t = sm.symbols("t")
n = sm.symbols("n", integer=True)
L = 20
f1 = (2/L)*x*sin(n*np.pi*x/20)
f2 = (2/L)*(20-x)*sin(n*np.pi*x/20)
fint1 = sm.integrate(f1,(x,0,10))
fint2 = sm.integrate(f2,(x,10,20))
D = 0.475
g = (fint1+fint2)*sin(n*np.pi*x/20)*exp(-(n**2)*(np.pi**2)*D*t/400).nsimplify()
print('')
sm.pretty_print(g)
#gn = g.subs({n:1})
s = 0
for c in range(10):
s += g.subs({n:c})
print(s)
print('')
print('The function u(x,t) : ')
print('')
sm.pretty_print(s)
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
# defining surface and axes
x = np.linspace(0, 20, 50)
t = np.linspace(0, 10, 50)
def u(x, t):
return s
X, T = np.meshgrid(x, t)
Z = u(X, T)
print('')
print('u(x,t)')
print(Z)
#fig = plt.figure()
# syntax for 3-D plotting
#ax = plt.axes(projection='3d')
# syntax for plotting
#ax.plot_wireframe(X,T,Z, cmap='viridis',edgecolor='green')
#ax.set_title('Wireframe')
#plt.show()
1条答案
按热度按时间i1icjdpr1#
**第一个选项:**使用
sm.lambdify
将s
转换为数值函数,以便稍后进行计算。**第二种选择:**使用SymPy Plotting Backend。有了它,你可以避免使用
sm.lambdify
和Numpy。一切都由模块处理。此模块的优点是您可以使用不同的打印库。例如,Plotly在3D可视化方面更好: