Solving ODE with input vector using Matrix Exponential



In the manual section 20.3, there’s an example of solving a linear ODE with the Matrix Exponential method. The example is of the type: dx/dt = Ax, where x is a vector of state variables, and A holds the parameters to be estimated (describing the dynamics of the state space model).

In Stan, can a linear ODE with an input vector be solved with Matrix Exponential as well? That’s a linear ODE of the type: dx/dt = Ax + Bu, where u is the input vector (holding data) and B is a matrix of parameters describing how the input signals (u) are entering the system.

Lukas Lundström


I think in general you’d still need an ODE solver for this ( Matrix exponential only gets you part of the way.

But if u(t) has some special form, maybe you can do the integration manually.

Not sure how separating things out would work autodiff-wise. To do the integral of the input signal you’d need a lot of outputs from your matrix exponential, which’d mean a lot of separate little matrix exponentials, which might end up being slower than the full ODE solve but I don’t really know.

Hope that helps!


Thanks @bbbales2,

Yes, solving the input signal part of the “General State Equation Solution” of your link looks quite calculation heavy. I guess the best way to see if it is any faster would be to test it out. But, I’ll stick to the numerical integrator for now.


If v = Bu is constant, then:

x = matrix_exp(tA) * (inv(A) * v + x0) - inv(A) * v. You can check this solves the equation by taking the derivative.

If u is time dependent, I think the linearity breaks and you need to use the numerical integrator.