A stable method to solve the Matrix Sturm-Liouville Equation for inhomogeneous layered structures