用Python计算(符号)jacobian的逆

时间:2019-12-10 15:12:02

标签: python matrix

  

我想为联合扩展卡尔曼滤波器计算jacboian的逆。我定义了一个函数来计算jacobian并使用来自sympy的反函数,我可以计算反函数。但是当我使用我的代码   出现此错误:

TypeError: No loop matching the specified signature and casting
was found for ufunc inv
  

我希望有人能帮助我。谢谢大家:)

xk, Ta, Pt, dt, C, Rr = symbols('xk Ta Pt dt C Rr')
def kf_jacobian(xk, C, Rr, dt, Ta, Pt):
    alpha = 1 - exp(-1 / (C * Rr) * dt)  # Zero-Order-Hold-Diskretisierung
    Hochzahl = -1 / (C * Rr) * dt  # Exponent
    beta = Rr * (1 - exp(Hochzahl))
    A = Matrix([[(1-10**(-3)*alpha) * xk + 10**(-3)*(alpha * Ta + beta * Pt)],[C], [Rr]])
    Y = Matrix([xk, C, Rr])
    Aj = A.jacobian(Y)
    return Aj


(Aj) = kf_jacobian(xk, C, Rr, dt, Ta, Pt)
Q = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])
P = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])
H = 1
R = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])

#xk = 18
#Ta = 30.0                                                            # Außentemperatur in KiloKelvin
#Pt = 30.0                                                            # Gesamte Wärmeleistung in MegaWatt
#dt = 0.1                                                             # Sampletime
#C = 20                                                               # Wärmekapazität in KiloJoule/Kelvin
#Rr = 4
#print(Aj)
Pn = dot(Aj, dot(P, Aj.T)) + Q
IS = R + dot(H, dot(Pn, H))


S = inv(Aj)
print(Aj)

1 个答案:

答案 0 :(得分:0)

在这种情况下的问题是我用sympy.jacobian生成了我的Jacobian,但是我使用了numpy.inverse。这些可能不兼容。现在,我只使用了sympy函数,而python可以计算逆函数。

xk, Ta, Pt, dt, C, Rr = symbols('xk Ta Pt dt C Rr')
def kf_jacobian(xk, C, Rr, dt, Ta, Pt):
    alpha = 1 - exp(-1 / (C * Rr) * dt)  # Zero-Order-Hold-Diskretisierung
    Hochzahl = -1 / (C * Rr) * dt  # Exponent
    beta = Rr * (1 - exp(Hochzahl))
    A = Matrix([[(1-10**(-3)*alpha) * xk + 10**(-3)*(alpha * Ta + beta * Pt)],[C], [Rr]])
    Y = Matrix([xk, C, Rr])
    Aj = A.jacobian(Y)
    return Aj


(Aj) = kf_jacobian(xk, C, Rr, dt, Ta, Pt)
Q = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])
P = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])
H = 1
R = Matrix([[0.01, 0, 0], [0, 0.01, 0], [0, 0, 0.01]])

#xk = 18
#Ta = 30.0                                                            # Außentemperatur in KiloKelvin
#Pt = 30.0                                                            # Gesamte Wärmeleistung in MegaWatt
#dt = 0.1                                                             # Sampletime
#C = 20                                                               # Wärmekapazität in KiloJoule/Kelvin
#Rr = 4
#print(Aj)
Pn = dot(Aj, dot(P, Aj.T)) + Q
IS = R + dot(H, dot(Pn, H))


S = Aj.inv()
print(S)
Return:
Matrix([[1/(0.999 + 0.001*exp(-dt/(C*Rr))), (0.001*Pt*dt*exp(-dt/(C*Rr))/C**2 + 0.001*Ta*dt*exp(-dt/(C*Rr))/(C**2*Rr) - 0.001*dt*xk*exp(-dt/(C*Rr))/(C**2*Rr))/(0.999 + 0.001*exp(-dt/(C*Rr))), (-0.001*Pt*(1 - exp(-dt/(C*Rr))) + 0.001*Pt*dt*exp(-dt/(C*Rr))/(C*Rr) + 0.001*Ta*dt*exp(-dt/(C*Rr))/(C*Rr**2) - 0.001*dt*xk*exp(-dt/(C*Rr))/(C*Rr**2))/(0.999 + 0.001*exp(-dt/(C*Rr)))], [0, 1, 0], [0, 0, 1]])