计算Python中向量字段的卷曲并使用matplotlib进行绘制 [英] Calculate curl of a vector field in Python and plot it with matplotlib
问题描述
我需要计算向量场的卷曲并用 matplotlib 绘制它.我正在寻找的一个简单示例可以像这样:
I need to calculate the curl of a vector field and plot it with matplotlib. A simple example of what I am looking for could be put like that:
如何在 quiver3d_demo.py 在 matplotlib 库中?
How can I calculate and plot the curl of the vector field in the quiver3d_demo.py in the matplotlib gallery?
推荐答案
You can use sympy.curl()
to calculate the curl of a vector field.
示例:
假设 F(x,y,z) = y2zi - xyj + z2k,则:
Suppose F(x,y,z) = y2zi - xyj + z2k, then:
y
将是R[1]
,x
是R[0]
和z
是R[2]
- 3个轴的单位向量i、j、k,分别为
Rx
、<代码>Ry,Rz
.
y
would beR[1]
,x
isR[0]
andz
isR[2]
- the unit vectors i, j, k of the 3 axes, would be respectively
R.x
,R.y
,R.z
.
计算矢量场卷曲的代码为:
from sympy.physics.vector import ReferenceFrame
from sympy.physics.vector import curl
R = ReferenceFrame('R')
F = R[1]**2 * R[2] * R.x - R[0]*R[1] * R.y + R[2]**2 * R.z
G = curl(F, R)
在这种情况下,G等于 R_y ** 2 * R.y +(-2 * R_y * R_z-R_y)* R.z
或换句话说,
G = 0i + y2j + (-2yz-y)k.
In that case G would be equal to R_y**2*R.y + (-2*R_y*R_z - R_y)*R.z
or, in other words,
G = 0i + y2j + (-2yz-y)k.
要绘制它,您需要将上述结果转换为 3 个单独的函数;你,v,w.
To plot it you need to convert the above result into 3 separate functions; u,v,w.
(以下示例摘自此 matplotlib示例):
(example below adapted from this matplotlib example):
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.8))
u = 0
v = y**2
w = -2*y*z - y
ax.quiver(x, y, z, u, v, w, length=0.1)
plt.show()
最终结果是:
这篇关于计算Python中向量字段的卷曲并使用matplotlib进行绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!