diff --git a/DynamicModel.cc b/DynamicModel.cc
index 6e4e3ed5ceeb750923ea6b592decebb0fcdd6635..e8392957e6b676435c0a58c25ff2b43ec044fb24 100644
--- a/DynamicModel.cc
+++ b/DynamicModel.cc
@@ -1146,7 +1146,7 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const
 }
 
 void
-DynamicModel::writeDynamicCFile(const string &dynamic_basename) const
+DynamicModel::writeDynamicCFile(const string &dynamic_basename, const int order) const
 {
   string filename = dynamic_basename + ".c";
   ofstream mDynamicModelFile;
@@ -1181,6 +1181,9 @@ DynamicModel::writeDynamicCFile(const string &dynamic_basename) const
                     << "  double *residual, *g1, *v2, *v3;" << endl
                     << "  int nb_row_x, it_;" << endl
                     << endl
+                    << "  /* Check that no derivatives of higher order than computed are being requested */ " << endl
+                    << "  if (nlhs > " << order + 1 << ") " << endl
+                    << "    mexErrMsgTxt(\"Derivatives of higher order than computed have been requested\"); " << endl
                     << "  /* Create a pointer to the input matrix y. */" << endl
                     << "  y = mxGetPr(prhs[0]);" << endl
                     << endl
@@ -1868,7 +1871,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll) const
 }
 
 void
-DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll) const
+DynamicModel::writeOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order) const
 {
   /* Writing initialisation for M_.lead_lag_incidence matrix
      M_.lead_lag_incidence is a matrix with as many columns as there are
@@ -2062,10 +2065,21 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
     output << "M_.params = repmat(NaN," << symbol_table.param_nbr() << ", 1);" << endl;
 
   // Write number of non-zero derivatives
+  // Use -1 if the derivatives have not been computed
   output << "M_.NNZDerivatives = zeros(3, 1);" << endl
-         << "M_.NNZDerivatives(1) = " << NNZDerivatives[0] << ";" << endl
-         << "M_.NNZDerivatives(2) = " << NNZDerivatives[1] << ";" << endl
-         << "M_.NNZDerivatives(3) = " << NNZDerivatives[2] << ";" << endl;
+         << "M_.NNZDerivatives(1) = " << NNZDerivatives[0] << ";" << endl;
+  if (order > 1)
+    {
+      output << "M_.NNZDerivatives(2) = " << NNZDerivatives[1] << ";" << endl;
+      if (order > 2)
+	output << "M_.NNZDerivatives(3) = " << NNZDerivatives[2] << ";" << endl;
+      else
+	output << "M_.NNZDerivatives(3) = -1;" << endl;
+    }
+  else
+    output << "M_.NNZDerivatives(2) = -1;" << endl
+	   << "M_.NNZDerivatives(3) = -1;" << endl;
+
 }
 
 map<pair<int, pair<int, int > >, NodeID>
@@ -2422,7 +2436,7 @@ DynamicModel::collect_block_first_order_derivatives()
 }
 
 void
-DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll) const
+DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const
 {
   int r;
   string t_basename = basename + "_dynamic";
@@ -2445,7 +2459,7 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode
       writeSparseDynamicMFile(t_basename, basename);
     }
   else if (use_dll)
-    writeDynamicCFile(t_basename);
+    writeDynamicCFile(t_basename, order);
   else
     writeDynamicMFile(t_basename);
 }
diff --git a/DynamicModel.hh b/DynamicModel.hh
index f143355a621a4524a51908aeb55055c9ce0441e0..e5a8de360692ed45ba93c8613b119024cafd0fd0 100644
--- a/DynamicModel.hh
+++ b/DynamicModel.hh
@@ -108,7 +108,7 @@ private:
   void writeDynamicMFile(const string &dynamic_basename) const;
   //! Writes dynamic model file (C version)
   /*! \todo add third derivatives handling */
-  void writeDynamicCFile(const string &dynamic_basename) const;
+  void writeDynamicCFile(const string &dynamic_basename, const int order) const;
   //! Writes dynamic model file when SparseDLL option is on
   void writeSparseDynamicMFile(const string &dynamic_basename, const string &basename) const;
   //! Writes the dynamic model equations and its derivatives
@@ -247,13 +247,13 @@ public:
   void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
                      const eval_context_type &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode);
   //! Writes model initialization and lead/lag incidence matrix to output
-  void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll) const;
+  void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order) const;
 
   //! Adds informations for simulation in a binary file
   void Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename,
                              const int &num, int &u_count_int, bool &file_open, bool is_two_boundaries) const;
   //! Writes dynamic model file
-  void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll) const;
+  void writeDynamicFile(const string &basename, bool block, bool bytecode, bool use_dll, int order) const;
   //! Writes file containing parameters derivatives
   void writeParamsDerivativesFile(const string &basename) const;
   //! Converts to static model (only the equations)
diff --git a/ModFile.cc b/ModFile.cc
index 71c539bdf5b208df9acc9d0d4f686f86fbd4254e..5a5a52c7088f56b8fbc5e216092862086a4409a4 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -482,9 +482,9 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all
   if (dynamic_model.equation_number() > 0)
     {
       if (dynamic_model_needed)
-        dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll);
+        dynamic_model.writeOutput(mOutputFile, basename, block, byte_code, use_dll, mod_file_struct.order_option);
       else
-        dynamic_model.writeOutput(mOutputFile, basename, false, false, false);
+        dynamic_model.writeOutput(mOutputFile, basename, false, false, false, mod_file_struct.order_option);
       if (!byte_code && !no_static)
         static_model.writeOutput(mOutputFile, block);
     }
@@ -527,12 +527,12 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all
 
       if (dynamic_model_needed)
         {
-          dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll);
+          dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
           dynamic_model.writeParamsDerivativesFile(basename);
         }
       else
         {
-          dynamic_model.writeDynamicFile(basename, false, false, false);
+          dynamic_model.writeDynamicFile(basename, false, false, false, mod_file_struct.order_option);
           dynamic_model.writeParamsDerivativesFile(basename);
         }
     }