python - Sympy:如何计算矩阵相对于向量场的李导数

标签 python numpy vector sympy derivative

我有一个系统 (x'=f(x)+g(x)u),这样 f(x) 是 f:R3->R3g(x ) 是 g:R3->R(3x2)。 我的系统是

enter image description here enter image description here

如您所见,这是一个 MIMO 非线性控制系统,我希望找到我的系统的可控性矩阵。这种情况下的可控性矩阵由
制定 C=[g [f,g] [f,[f,g]] ..],
其中 [f,g] 表示 f 和 g 之间的括号操作。 这就是为什么我需要计算矩阵相对于矢量场的李导数,反之亦然。因为 [f,g]=fdg/dx-gdf/dx

在我的系统中,f 是 3x1,g 是 3x2,因为有两个输入可用。 我想用 Python 计算上面的矩阵 C。 我的系统是
f=sm.Matrix([[x1**2],[sin(x1)+x3**2],[cos(x3)+x1**2]])
g=sm.Matrix([[cos(x1),0],[x1**2,x2],[0,0]])

我的代码是:

from sympy.diffgeom import *
from sympy import sqrt,sin,cos

M     = Manifold("M",3)
P     = Patch("P",M)

coord          = CoordSystem("coord",P,["x1","x2","x3"])
x1,x2,x3       = coord.coord_functions()
e_x1,e_x2,e_x3 = coord.base_vectors()

f      = x1**2*e_x1 + (sin(x1)+x3**2)*e_x2 + (cos(x3) + x1**2)*e_x3
g      = (cos(x1))*e_x1+(x1**2,x2)*e_x2 + 0*e_x3

#h1    = x1
#h2    = x2
#Lfh1  = LieDerivative(f,h1)
#Lfh2  = LieDerivative(f,h2)
#print(Lfh1)
#print(Lfh2)

Lfg    = LieDerivative(f,g)
print(Lfg)

为什么我的代码没有给出正确答案?

最佳答案

您的代码中唯一的错误是由于用于多个输入的元组。要使 Sympy.diffgeom 中的 LieDerivative 正常工作,您需要正确定义矢量场。

对于单输入系统,您拥有的确切代码可以在没有元组的情况下工作,因此,在这种情况下,例如,如果您有 g = (cos(x1))*e_x1+x1**2*e_x2 + 0*e_x3

(即 g(x) 是只有第一列的 3 x 1 矩阵)。然后,进行上述更改,您将得到正确的李导数。

对于多输入情况,(如您的问题),您可以简单地将两列分成 g1 和 g2 并按照上述情况进行。这是有效的,因为对于多个输入的情况, See math here

其中 g_1 和 g_2 是两列。 Lgh 的最终结果是一个 1 x 2 矩阵,如果你有上面计算的两个结果(Lg1h 和 Lg2h),你基本上可以得到。

代码-

from sympy.diffgeom import *
from sympy import sqrt,sin,cos
from sympy import *
M = Manifold("M",3)
P = Patch("P",M)

coord          = CoordSystem("coord",P,["x1","x2","x3"])
x1,x2,x3       = coord.coord_functions()
e_x1,e_x2,e_x3 = coord.base_vectors()

f  = x1**2*e_x1 + (sin(x1)+x3**2)*e_x2 + (cos(x3) + x1**2)*e_x3
g1 = (cos(x1))*e_x1+(x1**2)*e_x2 + 0*e_x3
g2 = 0*e_x1+ x2*e_x2 + 0*e_x3
h = x1

Lg1h = LieDerivative(g1,h)
Lg2h = LieDerivative(g2,h)
Lgh = [Lg1h, Lg2h]
print(Lgh)

关于python - Sympy:如何计算矩阵相对于向量场的李导数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45255442/

相关文章:

python - 在 pandas 中添加带条件的列计算

python - 在 Python 2.7.3 中将字段名称分配给 numpy 数组

c++ - 复制 std::vector 但将 lambda 应用于每个元素

arrays - Rust向量(`Vec<T>`)与数组(`[T; n]`的性能

python - 如何在 Python Pandas 中对同一数据框中的两列执行操作?

python - scikit-learn, linearsvc - 如何从经过训练的 SVM 中获取支持向量?

python - 通过 sqlalchemy 添加对象抛出 KeyError : 'SQLALCHEMY_TRACK_MODIFICATIONS'

python - 在 python 中调用 C 库?

c++ - 将 std::vector 作为参数传递给函数时出现 EXC_BAD_ACCESS

python - punjab 无法连接到外部服务