diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 60192407e24622cb80c78e9fb19b493b5baae8a9..305e85f78e76c8d28d1b4a3b2e2bae6ca92bd6a2 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -2500,10 +2500,6 @@ ModelTree::writeSparseModelMFiles(const string &basename) const
          << "[T_order, T] = " << full_prefix << "resid_tt(y, x, params" << ss_arg << ", T_order, T);" << endl
          << "residual = NaN(" << equations.size() << ", 1);" << endl
          << d_sparse_output[0].str();
-  if constexpr(!dynamic)
-    output << "if ~isreal(residual)" << endl
-           << "    residual = real(residual)+imag(residual).^2;" << endl
-           << "end" << endl;
   output << "end" << endl;
   output.close();
 
@@ -2534,10 +2530,6 @@ ModelTree::writeSparseModelMFiles(const string &basename) const
          << "[T_order, T] = " << full_prefix << "g1_tt(y, x, params" << ss_arg << ", T_order, T);" << endl
          << "g1_v = NaN(" << jacobian_sparse_column_major_order.size() << ", 1);" << endl
          << d_sparse_output[1].str();
-  if constexpr(!dynamic)
-    output << "if ~isreal(g1_v)" << endl
-           << "    g1_v = real(g1_v)+2*imag(g1_v);" << endl
-           << "end" << endl;
   // On MATLAB < R2020a, sparse() does not accept int32 indices
   output << "if ~isoctave && matlab_ver_less_than('9.8')" << endl
          << "    sparse_rowval = double(sparse_rowval);" << endl
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 7b59a2871ad6fe2cda365009fd933f74bb62928f..fb15c88140eea5d3043b141414b20a5060086a98 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -227,9 +227,6 @@ StaticModel::writeStaticMFile(const string &basename) const
 
   ostringstream init_output, end_output;
   init_output << "residual = zeros(" << equations.size() << ", 1);";
-  end_output << "if ~isreal(residual)" << endl
-             << "  residual = real(residual)+imag(residual).^2;" << endl
-             << "end";
   writeStaticMFileHelper(basename, "static_resid", "residual", "static_resid_tt",
                          temporary_terms_derivatives[0].size(),
                          "", init_output, end_output,
@@ -238,9 +235,6 @@ StaticModel::writeStaticMFile(const string &basename) const
   init_output.str("");
   end_output.str("");
   init_output << "g1 = zeros(" << equations.size() << ", " << symbol_table.endo_nbr() << ");";
-  end_output << "if ~isreal(g1)" << endl
-             << "    g1 = real(g1)+2*imag(g1);" << endl
-             << "end";
   writeStaticMFileHelper(basename, "static_g1", "g1", "static_g1_tt",
                          temporary_terms_derivatives[0].size() + temporary_terms_derivatives[1].size(),
                          "static_resid_tt",