Python 极坐标图马格纳斯效应未显示正确数据
我想在极坐标图上画出绕旋转圆柱体流动的速度方程。(这些方程式来自安德森的《空气动力学基础》)。你可以在for循环语句中看到这两个方程式 我无法大声地把计算出的数据表示在极坐标图上。我已经尝试了我的每一个想法,但都一无所获。我确实检查了数据,这一个似乎都是正确的,因为它的行为应该是正确的 下面是我最后一次尝试的代码:Python 极坐标图马格纳斯效应未显示正确数据,python,matplotlib,polar-coordinates,Python,Matplotlib,Polar Coordinates,我想在极坐标图上画出绕旋转圆柱体流动的速度方程。(这些方程式来自安德森的《空气动力学基础》)。你可以在for循环语句中看到这两个方程式 我无法大声地把计算出的数据表示在极坐标图上。我已经尝试了我的每一个想法,但都一无所获。我确实检查了数据,这一个似乎都是正确的,因为它的行为应该是正确的 下面是我最后一次尝试的代码: import numpy as np import matplotlib.pyplot as plt RadiusColumn = 1.0 VelocityInfinity =
import numpy as np
import matplotlib.pyplot as plt
RadiusColumn = 1.0
VelocityInfinity = 10.0
RPM_Columns = 0.0#
ColumnOmega = (2*np.pi*RPM_Columns)/(60)#rad/s
VortexStrength = 2*np.pi*RadiusColumn**2 * ColumnOmega#rad m^2/s
NumberRadii = 6
NumberThetas = 19
theta = np.linspace(0,2*np.pi,NumberThetas)
radius = np.linspace(RadiusColumn, 10 * RadiusColumn, NumberRadii)
f = plt.figure()
ax = f.add_subplot(111, polar=True)
for r in xrange(len(radius)):
for t in xrange(len(theta)):
VelocityRadius = (1.0 - (RadiusColumn**2/radius[r]**2)) * VelocityInfinity * np.cos(theta[t])
VelocityTheta = - (1.0 + (RadiusColumn**2/radius[r]**2))* VelocityInfinity * np.sin(theta[t]) - (VortexStrength/(2*np.pi*radius[r]))
TotalVelocity = np.linalg.norm((VelocityRadius, VelocityTheta))
ax.quiver(theta[t], radius[r], theta[t] + VelocityTheta/TotalVelocity, radius[r] + VelocityRadius/TotalVelocity)
plt.show()
如您所见,我现在已将RPM设置为0。这意味着流动应该从左向右,并且在水平轴上对称。(气流应在气缸两侧以相同的方式流动。)但结果更像这样:
这完全是胡说八道。似乎有一个涡度,即使没有设置!更奇怪的是,当我只显示从0到pi/2的数据时,流量会发生变化
正如您从代码中看到的,我已经尝试使用单位向量,但显然这不是解决方法。如有任何有用的意见,我将不胜感激
谢谢 基本问题是极轴
轴对象的.quiver
方法仍然需要笛卡尔坐标系中的向量分量,因此您需要自己将θ和径向分量转换为x和y:
for r in range(len(radius)):
for t in range(len(theta)):
VelocityRadius = (1.0 - (RadiusColumn**2/radius[r]**2)) * VelocityInfinity * np.cos(theta[t])
VelocityTheta = - (1.0 + (RadiusColumn**2/radius[r]**2))* VelocityInfinity * np.sin(theta[t]) - (VortexStrength/(2*np.pi*radius[r]))
TotalVelocity = np.linalg.norm((VelocityRadius, VelocityTheta))
ax.quiver(theta[t], radius[r],
VelocityRadius/TotalVelocity*np.cos(theta[t])
- VelocityTheta/TotalVelocity*np.sin(theta[t]),
VelocityRadius/TotalVelocity*np.sin(theta[t])
+ VelocityTheta/TotalVelocity*np.cos(theta[t]))
plt.show()
然而,通过使用矢量化,您可以大大改进代码:您不需要在每个点上循环以获得所需的内容。与您的代码相同,但更清楚:
def pol2cart(th,v_th,v_r):
"""convert polar velocity components to Cartesian, return v_x,v_y"""
return v_r*np.cos(th) - v_th*np.sin(th), v_r*np.sin(th) + v_th*np.cos(th)
theta = np.linspace(0, 2*np.pi, NumberThetas, endpoint=False)
radius = np.linspace(RadiusColumn, 10 * RadiusColumn, NumberRadii)[:,None]
f = plt.figure()
ax = f.add_subplot(111, polar=True)
VelocityRadius = (1.0 - (RadiusColumn**2/radius**2)) * VelocityInfinity * np.cos(theta)
VelocityTheta = - (1.0 + (RadiusColumn**2/radius**2))* VelocityInfinity * np.sin(theta) - (VortexStrength/(2*np.pi*radius))
TotalVelocity = np.linalg.norm([VelocityRadius, VelocityTheta],axis=0)
VelocityX,VelocityY = pol2cart(theta, VelocityTheta, VelocityRadius)
ax.quiver(theta, radius, VelocityX/TotalVelocity, VelocityY/TotalVelocity)
plt.show()
很少有显著变化:
- 我将
endpoint=False
添加到theta
:由于您的函数在2*pi
中是周期性的,因此不需要绘制两次端点。请注意,这意味着当前有更多可见箭头;如果您想要原始行为,我建议您将NumberThetas
减少一
- 我将
[:,None]
添加到半径中:这将使其成为二维数组,因此速度定义中的后续操作将为您提供二维数组:不同的列对应不同的角度,不同的行对应不同的半径quiver
与数组值输入兼容,因此只需调用quiver
即可完成您的工作
- 由于速度现在是2d数组,我们需要在基本上是3d数组上调用
np.linalg.norm
,但是如果我们指定一个轴来处理,这是可以预期的
- 我定义了
pol2cart
辅助函数来完成从极坐标分量到笛卡尔分量的转换;这是没有必要的,但我觉得这样更清楚
最后一句话:我建议选择较短的变量名,以及没有大小写的变量名。这可能会让你的编码速度更快。Wow!谢谢,这太棒了!我会继续尝试你的建议。我只想说一句,我同意写出变量名需要更多的时间,但就我个人而言,我发现这会使代码更清晰10倍。一年后,我读了这篇文章,它就像读了一个故事。字母变量对我来说是个麻烦。无论如何,再次谢谢你,我会再看一遍。