python - 绘制卷曲,得到一个 AttributeError
问题描述
我有一个我想用matplotlib计算出来的磁场卷曲,但我最终得到了这个AttributeError。有什么我做错了吗?
我尝试将变量从使用参考框架 R[0] 更改为仅 x,但也使用笛卡尔坐标的参考框架合并向量。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
import sympy
from sympy import Symbol, diff, Array, sin, cos
from sympy import init_printing
from sympy.physics.vector import curl, ReferenceFrame
init_printing()
alpha = Symbol('\u03B1')
B0 = Symbol('B0')
R = ReferenceFrame('R')
# In order to get the curl, setting it as a vector
V = 0*R.x + B0*sin(alpha*R[0])*R.y + B0*cos(alpha*R[0])*R.z
# Calculates the curl of the vector, which results in a vector of
# alpha*B, which is correct
C = curl(V,R)
print('The curl of B is:',0*R.x + C,)
fig = plt.figure()
ax = fig.gca(projection = '3d')
x,y,z = np.meshgrid(np.arange(0.01, 1, 0.2),
np.arange(0.01, 1, 0.2),
np.arange(0.01, 1, 0.2))
u = 0*R.x
v = B0*alpha*sin(alpha*x)*R.y
w = B0*alpha*cos(alpha*x)*R.z
ax.quiver(x, y, z, u, v, w, length = 0.1)
plt.show()
我期待一个显示矢量场的 3D 图;下面是错误的回溯。
AttributeError Traceback (most recent
call last)
<ipython-input-12-5974c739f1f4> in <module>
10 np.arange(0.01, 1, 0.2))
11 u = 0*R.x
---> 12 v = B0*alpha*sin(alpha*x)*R.y
13 w = B0*alpha*cos(alpha*x)*R.z
14
~/anaconda3/lib/python3.7/site-packages/sympy/core/function.py in
__new__(cls, *args, **options)
440
441 evaluate = options.get('evaluate',
global_evaluate[0])
--> 442 result = super(Function, cls).__new__(cls, *args,
**options)
443 if evaluate and isinstance(result, cls) and
result.args:
444 pr2 = min(cls._should_evalf(a) for a in
result.args)
~/anaconda3/lib/python3.7/site-packages/sympy/core/function.py in
__new__(cls, *args, **options)
249
250 if evaluate:
--> 251 evaluated = cls.eval(*args)
252 if evaluated is not None:
253 return evaluated
~/anaconda3/lib/python3.7/site-
packages/sympy/functions/elementary/trigonometric.py in eval(cls,
arg)
293 return arg._eval_func(cls)
294
--> 295 if arg.could_extract_minus_sign():
296 return -cls(-arg)
297
AttributeError: 'ImmutableDenseNDimArray' object has no attribute
'could_extract_minus_sign'
解决方案
看看这个答案,下面的代码应该向前迈进了一步。
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d # noqa
from sympy import cos, sin, Symbol
from sympy import init_printing
from sympy.physics.vector import curl, ReferenceFrame
init_printing()
alpha = Symbol('\u03B1')
B0 = Symbol('B0')
R = ReferenceFrame('R')
# In order to get the curl, setting it as a vector
V = 0 * R.x + B0 * sin(alpha * R[0]) * R.y + B0 * cos(alpha * R[0]) * R.z
# Calculates the curl of the vector, which results in a vector of alpha * B,
# which is correct
C = curl(V, R)
print('The curl of B is:', C)
fig = plt.figure()
ax = fig.gca(projection='3d')
x, y, z = np.meshgrid(np.arange(0.01, 1, 0.2),
np.arange(0.01, 1, 0.2),
np.arange(0.01, 1, 0.2))
B0 = 0.2
alpha = 5
u = 0
v = B0 * alpha * np.sin(alpha * x)
w = B0 * alpha * np.cos(alpha * x)
ax.quiver(x, y, z, u, v, w, length=0.1)
plt.show()
另外,我想应该有一种方法可以像这样计算 u、v 和 w C.dot(R.y).subs([(B0, 1), (alpha, 1), (R[0], x)])
,但我对 sympy 不够熟悉,无法弄清楚为什么它会返回一个 Sympy 表达式。顺便说一句,你为什么不使用 numpy ?
推荐阅读
- php - 动态获取数据列表选项的 ID
- angular - 如何使用 angular2 在 API 中存储 base64 图像字符串?
- swift - 重复调用 CoreData 后应用程序变慢
- android - 无法在 viewpager kotlin 中查看文本
- excel - 任务完成excel功能
- google-chrome - 编码的 UI 不会在 Chrome 中启动测试
- javascript - Possibility of adding html, css, js, ... to Go server?
- google-analytics - GTM 与 GA 中会话数据的差异
- spring - 未调用 Spring Boot Rest API 过滤器
- excel - vba MID 函数使我的撇号不可见