gpt4 book ai didi

python - 使用箭袋绘制矢量场

转载 作者:行者123 更新时间:2023-12-04 14:04:00 25 4
gpt4 key购买 nike

我正在尝试使用组件重现二维矢量图

 v = 100/a * exp(-1/a^2 * ((x+0.55)^2+y^2))(-y,x) - 100/a * exp(-1/a^2 * ((x-0.55)^2+y^2))(-y,x)

这是我的代码。它没有给出我想要的 map (见附件 vector map)。有人可以帮我吗?

import numpy as np
import matplotlib.pyplot as plt
import math

grid_resolution = 25
grid_size = 2*grid_resolution+1
a = 0.2
x = np.linspace(-1,1,grid_size)
y = np.linspace(-1,1,grid_size)
X,Y = np.meshgrid(x, y)

vx = np.zeros((grid_size,grid_size))
vy = np.zeros((grid_size,grid_size))

for i in range(0,grid_size):
for j in range(0,grid_size):
x0 = x[j]
y0 = y[i]
xx = (x0 + 0.55) ** 2 + y0 ** 2
yy = (x0 - 0.55) ** 2 + y0 ** 2
expf1 = math.exp(-xx / (a ** 2))
expf2 = math.exp(-yy / (a ** 2))
vx[i,j] = 100 / a * (-expf1 + expf2) * y0
vy[i,j] = 100 / a * (expf1 - expf2) * x0

fig, ax = plt.subplots()
ax.quiver(X, Y, vx, vy)
ax.set_aspect('equal')

plt.show()

最佳答案

在上一段中,当您计算 vx[i,j]vy[i,j] 时,您正在计算 ( x0, y0),而你应该在当前点计算它,所以 (x0 ± 0.55, y0)。此外,您应该更改 vxvy 的符号,以便绘制一个与您链接的矢量场类似的矢量场。

import numpy as np
import matplotlib.pyplot as plt
import math

grid_resolution = 25
grid_size = 2*grid_resolution + 1
a = 0.2
x = np.linspace(-1, 1, grid_size)
y = np.linspace(-1, 1, grid_size)
X, Y = np.meshgrid(x, y)

vx = np.zeros((grid_size, grid_size))
vy = np.zeros((grid_size, grid_size))

for i in range(0, grid_size):
for j in range(0, grid_size):
x0 = x[j]
y0 = y[i]
xx = (x0 + 0.55)**2 + y0**2
yy = (x0 - 0.55)**2 + y0**2
expf1 = math.exp(-xx/(a**2))
expf2 = math.exp(-yy/(a**2))
vx[i, j] = -100/a*(-expf1 + expf2)*y0
if x0 > 0:
vy[i, j] = -100/a*(expf1 - expf2)*(x0 - 0.55)
else:
vy[i, j] = -100/a*(expf1 - expf2)*(x0 + 0.55)

fig, ax = plt.subplots()
ax.quiver(X,Y,vx,vy)
ax.set_aspect('equal')

plt.show()

enter image description here

关于python - 使用箭袋绘制矢量场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69094812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com