【问题标题】:Graphing a Curl, got an AttributeError绘制卷曲,得到一个 AttributeError
【发布时间】:2019-04-23 13:16:03
【问题描述】:

我有一个我想用 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'

【问题讨论】:

    标签: python matplotlib sympy


    【解决方案1】:

    看看这个answer,下面的代码应该会向前迈出一步。

    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()
    

    另外,我想应该有一种方法可以像C.dot(R.y).subs([(B0, 1), (alpha, 1), (R[0], x)]) 这样计算 u、v 和 w,但我对 sympy 不够熟悉,无法弄清楚为什么它会返回一个 Sympy 表达式。顺便说一句,你为什么不使用 numpy 呢?

    【讨论】:

    • 我通常会使用 numpy,但我想不出一种方法来在 numpy 中进行 curl 而不会出现几个错误。
    • 我会鼓励你熟悉 numpy (docs.scipy.org/doc/numpy/reference/generated/…) 的梯度函数。在一个二维场上尝试它,你可以分析地计算出卷曲,然后考虑一个三维场。一旦您对使用该功能有信心,将其应用到您自己的数据中,应该不会有任何错误。
    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2016-01-18
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多