如何用Odeint解决状态空间模型?

时间:2017-11-03 02:16:24

标签: c++ eigen solver ode odeint

我正在尝试使用Eigen和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");

1 个答案:

答案 0 :(得分:0)

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

您应该通过某种插值函数实现U,在最简单的情况下,作为步长函数计算来自t的某个索引,并返回该索引的U值。像

这样的东西
i = (int)(t/U_step)
dxdt = A*x + B*U.col(i);