如何使用Odeint求解状态空间模型

How to solve a state space model with Odeint?

本文关键字:状态空间 模型 何使用 Odeint      更新时间:2023-10-16

我正在尝试使用特征和Odeint实现状态空间模型的数值模拟。我的麻烦是我需要引用控制数据U(积分前预定义),以便正确求解状态空间模型的Ax + Bu部分。我试图通过使用计数器来跟踪当前时间步长来实现这一点,但无论出于何种原因,每次 Odeint 调用系统函数时,它都会重置为零。

我将如何解决这个问题?我对状态空间系统进行建模的方法是否存在缺陷?

我的系统

struct Eigen_SS_NLTIV_Model
{
    Eigen_SS_NLTIV_Model(matrixXd &ssA, matrixXd &ssB, matrixXd &ssC, 
           matrixXd &ssD, matrixXd &ssU, matrixXd &ssY)
                :A(ssA), B(ssB), C(ssC), D(ssD), U(ssU), Y(ssY)
    {
        Y.resizeLike(U);
        Y.setZero();
        observerStep = 0;
        testPtr = &observerStep;
    }
    /* Observer Function:*/
    void operator()(matrixXd &x, double t)
    {
        Y.col(observerStep) = C*x + D*U.col(observerStep);
        observerStep += 1;
    }
    /* System Function:
     * ONLY the mathematical description of the system dynamics may be placed
     * here. Any data placed in here is destroyed after each iteration of the
     * stepper.
     */
    void operator()(matrixXd &x, matrixXd &dxdt, double t)
    {
        dxdt = A*x + B*U.col(*testPtr);
        //Cannot reference the variable "observerStep" directly as it gets reset 
        //every time this is called. *testPtr doesn't work either.
    }
    int observerStep;
    int *testPtr;
    matrixXd &A, &B, &C, &D, &U, &Y; //Input Vectors
};

我的 ODE 求解器设置

const double t_end = 3.0;
const double dt = 0.5;
int steps = (int)std::ceil(t_end / dt) + 1;
matrixXd A(2, 2), B(2, 2), C(2, 2), D(2, 2), x(2, 1);
matrixXd U = matrixXd::Constant(2, steps, 1.0);
matrixXd Y;
A << -0.5572, -0.7814, 0.7814, 0.0000;
B << 1.0, -1.0, 0.0, 2.0;
C << 1.9691, 6.4493, 1.9691, 6.4493;
D << 0.0, 0.0, 0.0, 0.0;
x << 0, 0;
Eigen_SS_NLTIV_Model matrixTest(A, B, C, D, U, Y);
odeint::integrate_const(odeint::runge_kutta4<matrixXd, double, matrixXd, double,
    odeint::vector_space_algebra>(),
    matrixTest, x, 0.0, t_end, dt, matrixTest);
//Ignore these two functions. They are there mostly for debugging.
writeCSV<matrixXd>(Y, "Y_OUT.csv");
prettyPrint<matrixXd>(Y, "Out Full");

使用经典的 Runge-Kutta 您知道您的 ODE 模型函数每步调用 4 次,次数t, t+h/2, t+h/2, t+h .对于实现自适应步长的其他求解器,您无法提前知道 ODE 模型函数的调用t

您应该通过某种插值函数实现U,在最简单的情况下是 step 函数,它从t计算某个索引并返回该索引的U值。类似的东西

i = (int)(t/U_step)
dxdt = A*x + B*U.col(i);