diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index 21df8f0f2e9206e81aea6bbf207aed52f4c7bd6f..78b2d33edcf97c7625d4a23ee61a61da9fc203f9 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -1754,8 +1754,10 @@ ModelTree::writeDynamicCFile(const string &dynamic_basename) const
                     << "  g2 = NULL;" << endl
                     << " if (nlhs >= 3)" << endl
                     << "  {" << endl
-                    << "     /* Set the output pointer to the output matrix g2. */" << endl
-                    << "     plhs[2] = mxCreateDoubleMatrix(" << equations.size() << ", " << variable_table.getDynJacobianColsNbr(computeJacobianExo)*variable_table.getDynJacobianColsNbr(computeJacobianExo) << ", mxREAL);" << endl
+                    << "     /* Set the output pointer to the output matrix g2. */" << endl;
+  int g2_ncols = variable_table.getDynJacobianColsNbr(computeJacobianExo)*variable_table.getDynJacobianColsNbr(computeJacobianExo);
+  mDynamicModelFile << "     plhs[2] = mxCreateSparse(" << equations.size() << ", " << g2_ncols << ", "
+                    << 5*g2_ncols << ", mxREAL);" << endl
                     << "     /* Create a C pointer to a copy of the output matrix g1. */" << endl
                     << "     g2 = mxGetPr(plhs[2]);" << endl
                     << "  }" << endl