Solution to Linear Time-invariant Matrix ODE $\frac{d\mathbf{P}}{dt} = \mathbf{A}\mathbf{P} + \mathbf{B}$ Do we have a solution for the following matrix ODE:
$$\frac{d\mathbf{P}}{dt} = \mathbf{A}\mathbf{P} + \mathbf{B}$$
where all matrices are square.
My guess is:
$$
\mathbf{P}(t) = \exp{(\mathbf{A}t)}\mathbf{P}(0) + \mathbf{A}^{-1}[\exp{(\mathbf{A}t)}\mathbf{B} - \mathbf{B}]
$$
when $\mathbf{A}$ is invertible. I am not sure it is correct and not clear when $\mathbf{A}$ is not full rank.
 A: Assuming $A$ is constant (i.e. independent of $t$), you can use $\exp(-At)$ as an integrating factor; left-multiplying by it gives $\frac{d}{dt} \bigl( e^{-At} P(t) \bigr) = e^{-At} B(t)$, so that
$$
e^{-At} P(t) - P(0) = \int_0^t e^{-As} B(s) \, ds
,
$$
and hence
$$
P(t) = e^{At} P(0) + \int_0^t e^{A(t-s)} B(s) \, ds
,
$$
which holds in general, and reduces to your formula if $A$ is invertible and $B$ is constant.
A: Guessing the solution is a time-honored way of solving differential equations.
Prove that your ansatz is correct by calculating
its time derivative
$$\eqalign{
AP &= Ae^{At}P_0 + e^{At}B-B \quad\implies\quad
 &AP+B = Ae^{At}P_0 + e^{At}B \\
A\dot P &= A^2e^{At}P_0 + Ae^{At}B \\
 &= A(AP+B) \qquad &\big({\rm Eq\;}{\tt\#1}\big)  \\
\dot P &= AP+B
 \qquad &\big({\rm QED}\big) \\\\
}$$

If $A$ is singular, try substituting
the pseudoinverse $A^+$
$$\eqalign{
P &= e^{At}P_0 + A^+(e^{At}-I)\,B
\qquad&\big({\rm ansatz}\big) \\
AP &= Ae^{At}P_0 + (e^{At}-I)\,B 
\qquad&\big({\rm recall}\!:\,AA^+A=A\big) \\
A\dot P &= A^2e^{At}P_0 + Ae^{At}B
\qquad&\big({\rm time\;derivative}\big) \\
 &= A(AP+B)
\qquad&\big({\rm simplify}\big) \\
}$$
So this assumption yielded a solution of
$\big({\rm Eq\;}{\tt\#1}\big)$
$$A\dot P = A(AP+B)\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad$$
if not the original ODE.
