矢量场的计算与绘制

2024-05-26 06:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用下面的公式为一个给定的物体画一个势场:

U=-α_goal*e^(-((x-x_goal )^2/a_goal +(y-y_goal^2)/b_goal ) )

使用以下代码

    # Set limits and number of points in grid
    xmax = 10.0
    xmin = -xmax
    NX = 20
    ymax = 10.0
    ymin = -ymax
    NY = 20
    # Make grid and calculate vector components
    x = linspace(xmin, xmax, NX)
    y = linspace(ymin, ymax, NY)
    X, Y = meshgrid(x, y)
    x_obstacle = 0
    y_obstacle = 0
    alpha_obstacle = 1
    a_obstacle = 1
    b_obstacle = 1
    P = -alpha_obstacle * exp(-(X - x_obstacle)**2 / a_obstacle + (Y - y_obstacle)**2 / b_obstacle)
    Ey,Ex = gradient(P)
    print Ey
    print Ex

    QP = quiver(X, Y, Ex, Ey)

    show()

此代码计算一个潜在字段。我怎样才能很好地画出这个势场呢?另外,给定一个势场,把它转换成矢量场的最佳方法是什么?(矢量场是势场的负梯度。)

我很感激你的帮助。

我试过使用np.gradient(),但结果不是我所期望的:

enter image description here

我所期望的是这样的: enter image description here

编辑: 更改代码中的两行后:

y, x = np.mgrid[500:-100:200j, 1000:-100:200j] 
p = -1 * np.exp(-((x - 893.6)**2 / 1000 + (y - 417.35)**2 / 1000))

我有一个不正确的绘图:它似乎是左右颠倒的(箭头似乎在正确的位置,但不是字段):enter image description here 编辑: 修改为y, x = np.mgrid[500:-100:200j, -100:1000:200j]知道原因吗?


Tags: and代码npgridexxminymaxnx
1条回答
网友
1楼 · 发布于 2024-05-26 06:21:11

首先,让我们在一个规则的网格上计算它,类似于您的示例代码。(另一方面,在计算公式的代码中有一个错误。在exp中缺少一个负数:

import numpy as np
import matplotlib.pyplot as plt

# Set limits and number of points in grid
y, x = np.mgrid[10:-10:100j, 10:-10:100j]

x_obstacle, y_obstacle = 0.0, 0.0
alpha_obstacle, a_obstacle, b_obstacle = 1.0, 1e3, 2e3

p = -alpha_obstacle * np.exp(-((x - x_obstacle)**2 / a_obstacle
                               + (y - y_obstacle)**2 / b_obstacle))

接下来,我们需要计算梯度(这是一个简单的有限差分,与解析计算上述函数的导数相反):

# For the absolute values of "dx" and "dy" to mean anything, we'll need to
# specify the "cellsize" of our grid.  For purely visual purposes, though,
# we could get away with just "dy, dx = np.gradient(p)".
dy, dx = np.gradient(p, np.diff(y[:2, 0]), np.diff(x[0, :2]))

现在我们可以绘制一个“颤动”图,但是,结果可能与您预期的不太一样,因为箭头显示在网格上的每个点上:

fig, ax = plt.subplots()
ax.quiver(x, y, dx, dy, p)
ax.set(aspect=1, title='Quiver Plot')
plt.show()

enter image description here

让我们把箭调大一点。最简单的方法是绘制每个第n个箭头并让matplotlib处理自动缩放。我们每三点用一次。如果您需要更少、更大的箭头,请将3改为更大的整数。

# Every 3rd point in each direction.
skip = (slice(None, None, 3), slice(None, None, 3))

fig, ax = plt.subplots()
ax.quiver(x[skip], y[skip], dx[skip], dy[skip], p[skip])
ax.set(aspect=1, title='Quiver Plot')
plt.show()

enter image description here

更好,但是那些箭还是很难看到。一种更好的可视化方法可能是使用覆盖黑色渐变箭头的图像打印:

skip = (slice(None, None, 3), slice(None, None, 3))

fig, ax = plt.subplots()
im = ax.imshow(p, extent=[x.min(), x.max(), y.min(), y.max()])
ax.quiver(x[skip], y[skip], dx[skip], dy[skip])

fig.colorbar(im)
ax.set(aspect=1, title='Quiver Plot')
plt.show()

enter image description here

理想情况下,我们希望使用不同的颜色映射或更改箭头颜色。我把那部分留给你。您还可以考虑等高线图(ax.contour(x, y, p))或流线图(ax.streamplot(x, y, dx, dy)。举个简单的例子:

fig, ax = plt.subplots()

ax.streamplot(x, y, dx, dy, color=p, density=0.5, cmap='gist_earth')

cont = ax.contour(x, y, p, cmap='gist_earth')
ax.clabel(cont)

ax.set(aspect=1, title='Streamplot with contours')
plt.show()

enter image description here

……为了让自己变得更漂亮:

from matplotlib.patheffects import withStroke

fig, ax = plt.subplots()

ax.streamplot(x, y, dx, dy, linewidth=500*np.hypot(dx, dy),
              color=p, density=1.2, cmap='gist_earth')

cont = ax.contour(x, y, p, cmap='gist_earth', vmin=p.min(), vmax=p.max())
labels = ax.clabel(cont)

plt.setp(labels, path_effects=[withStroke(linewidth=8, foreground='w')])

ax.set(aspect=1, title='Streamplot with contours')
plt.show()

enter image description here

相关问题 更多 >