如何检查雅可比是否正确

时间:2017-07-04 23:18:30

标签: python numpy optimization scipy integration

我很高兴将一个简单的ODE与scipy.odeint集成在一起。这是我的整合功能:

def f(self, x, t_0):
    h = x[0:3]
    f = x[3:6]

    der = []

    der.append((self.q_0 - f[0] * self.u[0] * self.k[0] * numpy.sign(h[0] - h[1]) * numpy.sqrt(abs(h[0] - h[1]))) / self.A[0])
    der.append((f[0] * self.u[0] * self.k[0] * numpy.sign(h[0] - h[1]) * numpy.sqrt(abs(h[0] - h[1])) -
                f[1] * self.u[1] * self.k[1] * numpy.sign(h[1]) * numpy.sqrt(abs(h[1]))) / self.A[1])
    der.append((f[1] * self.u[1] * self.k[1] * numpy.sign(h[1] - h[2]) * numpy.sqrt(abs(h[1] - h[2])) -
                f[2] * self.u[2] * self.k[2] * numpy.sign(h[2]) * numpy.sqrt(abs(h[2]))) / self.A[2])
    der.append(0)
    der.append(0)
    der.append(0)

    return numpy.array(der)

如果我调用scipy.integrate.odeint,除了我决定提供Jacobian以便我可以更快地进行集成之外,一切正常。这是我手工弄清楚的雅各比人:

def F(self, x, t_0):
    h = x[0:3]
    f = x[3:6]

    u = self.u
    k = self.k
    A = self.A

    result = numpy.zeros((6, 6))

    sqrt_diff_h0_h1 = numpy.sign(h[0] - h[1]) * numpy.sqrt(abs(h[0] - h[1]))
    sqrt_diff_h1_h2 = numpy.sign(h[1] - h[2]) * numpy.sqrt(abs(h[1] - h[2]))
    sqrt_diff_h1 = numpy.sign(h[1]) * numpy.sqrt(abs(h[1]))
    sqrt_diff_h2 = numpy.sign(h[2]) * numpy.sqrt(abs(h[2]))

    result[0][0] = -(u[0] * f[0] * k[0]) / (2 * A[0] * sqrt_diff_h0_h1)
    result[0][1] = (u[0] * f[0] * k[0]) / (2 * A[0] * sqrt_diff_h0_h1)
    result[0][3] = -(u[0] * k[0] * sqrt_diff_h0_h1) / A[0]
    result[1][0] = (u[0] * f[0] * k[0]) / (2 * A[1] * sqrt_diff_h0_h1)
    result[1][1] = -((u[0] * f[0] * k[0] * A[1]) / (2 * sqrt_diff_h0_h1)) - \
                   ((u[1] * f[1] * k[1] * A[1]) / (2 * sqrt_diff_h1))
    result[1][3] = (u[0] * k[0] * sqrt_diff_h0_h1) / A[1]
    result[1][4] = -(u[1] * k[1] * sqrt_diff_h1) / A[1]
    result[2][1] = (u[1] * f[1] * k[1]) / (2 * A[2] * sqrt_diff_h1_h2)
    result[2][2] = -((u[1] * f[1] * k[1] * A[2]) / (2 * sqrt_diff_h1_h2)) - \
                   ((u[2] * f[2] * k[2] * A[2]) / (2 * sqrt_diff_h2))
    result[2][4] = (u[1] * k[1] * sqrt_diff_h1_h2) / A[2]
    result[2][5] = -(u[2] * k[2] * sqrt_diff_h2) / A[2]

    return result

如果我向scipy.odeint.integrate提供F,实际上需要更多时间。当然,这并不意味着什么,但我想知道如何判断雅可比是否正确?我需要它用于线性化的其他目的。

0 个答案:

没有答案