0
我试图绘制基本位流(均匀,源出/吸入式,涡流式等)绘图与Python
我刚开始接触蟒蛇,因此我有点流线和速度势简化困惑。我下面this guide ..
我可以使用此功能
def cylinder_stream_function(U=1, R=1):
r = sympy.sqrt(x**2 + y**2)
theta = sympy.atan2(y, x)
return U * (r - R**2/r) * sympy.sin(theta)
和它的作品绘制流线的流动围绕圆柱体。但是,当我改变return语句
return U * r * sympy.cos(theta)
均匀流动,我得到
Traceback (most recent call last):
File "test.py", line 42, in
<module>
plot_streamlines(ax, u, v)
File "test.py", line 32, in plot_streamlines
ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')
File "/usr/local/lib/python3.6/site-packages/matplotlib/__init__.py",
line 1710, in inner
return func(ax, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_axes.py",
line 4688, in streamplot
integration_direction=integration_direction)
File "/usr/local/lib/python3.6/site-packages/matplotlib/streamplot.py",
line 136, in streamplot
if (u.shape != grid.shape) or (v.shape != grid.shape):
AttributeError: 'int' object has no attribute 'shape'
我检查返回的对象类型下面的错误,这是<class 'sympy.core.mul.Mul'>
与第一return语句和<class 'sympy.core.symbol.Symbol'>
与第二个。也许这与它为什么不起作用有关,但我不知道如何?
我绘制的流线具有以下
import numpy as np
import matplotlib.pyplot as plt
import sympy
from sympy.abc import x, y
def uniform_flow_stream_function(U=1):
r = sympy.sqrt(x**2 + y**2)
theta = sympy.atan2(y, x)
return U * r * sympy.sin(theta)
def velocity_field(psi):
u = sympy.lambdify((x, y), psi.diff(y), 'numpy')
v = sympy.lambdify((x, y), -psi.diff(x), 'numpy')
return u, v
def plot_streamlines(ax, u, v, xlim=(-4, 4), ylim=(-4, 4)):
x0, x1 = xlim
y0, y1 = ylim
# create a grid of values
Y, X = np.ogrid[y0:y1:100j, x0:x1:100j]
ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')
psi = uniform_flow_stream_function()
u, v = velocity_field(psi)
fig, ax = plt.subplots(figsize=(5, 5))
plot_streamlines(ax, u, v)
plt.show()
有人可以帮我明白为什么这不工作,我怎样才能得到它的工作?谢谢!
下一次:请使用代码块格式化回溯:这使得它更好地阅读。 – Evert