diff --git a/matlab/getPowerDeriv.m b/matlab/getPowerDeriv.m
new file mode 100644
index 0000000000000000000000000000000000000000..e87eabe1e026cebbf2361d8b8b749ba92892310c
--- /dev/null
+++ b/matlab/getPowerDeriv.m
@@ -0,0 +1,42 @@
+function dxp=getPowerDeriv(x,p,k)
+%function dxp=getPowerDeriv(x,p,k)
+% The k-th derivative of x^p
+%
+% INPUTS
+%    x: base
+%    p: power
+%    k: derivative order
+%
+% OUTPUTS
+%    dxp: k-th derivative of x^p
+%
+% SPECIAL REQUIREMENTS
+%    none
+
+% Copyright (C) 2011 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+if (abs(x) < 1e-12) && (p > 0) && (k >= p) && (abs(p - round(p)) < 1e-12)
+    dxp = 0;
+else
+    dxp = x^(p-k);
+    for i=0:k-1
+        dxp = dxp*p;
+        p = p-1;
+    end
+end
+end
diff --git a/preprocessor/DataTree.cc b/preprocessor/DataTree.cc
index 87052a6e18ecc511fc8531d8cd5e2063e2e8bce7..267a97e60bf4ac266510ac120319cf1d49d49d12 100644
--- a/preprocessor/DataTree.cc
+++ b/preprocessor/DataTree.cc
@@ -660,10 +660,7 @@ DataTree::writePowerDerivCHeader(ostream &output) const
 void
 DataTree::writePowerDeriv(ostream &output, bool use_dll) const
 {
-  if (!isBinaryOpUsed(oPowerDeriv))
-    return;
-
-  if (use_dll)
+  if (use_dll && isBinaryOpUsed(oPowerDeriv))
     output << "/*" << endl
            << " * The k-th derivative of x^p" << endl
            << " */" << endl
@@ -683,21 +680,4 @@ DataTree::writePowerDeriv(ostream &output, bool use_dll) const
            << "      return dxp;" << endl
            << "    }" << endl
            << "}" << endl;
-  else
-    output << endl
-           << "%" << endl
-           << "% The k-th derivative of x^p" << endl
-           << "%" << endl
-           << "function dxp=getPowerDeriv(x,p,k)" << endl
-           << "    if (abs(x) < " << NEAR_ZERO << ") && (p > 0) && (k >= p) && (abs(p - round(p)) < " << NEAR_ZERO << ")" << endl
-           << "        dxp = 0;" << endl
-           << "    else" << endl
-           << "        dxp = x^(p-k);" << endl
-           << "        for i=0:k-1" << endl
-           << "            dxp = dxp*p;" << endl
-           << "            p = p-1;" << endl
-           << "        end" << endl
-           << "    end" << endl
-           << "end" << endl;
-
 }
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 38f57595d9b61d852022cb698f5f926b3dfaea95..f37bf240bc81cf305817abbd2c6229a99ddf5f29 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -780,7 +780,6 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
           break;
         }
       output << "end" << endl;
-      writePowerDeriv(output, false);
       output.close();
     }
 }
@@ -1513,7 +1512,6 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const
 
   writeDynamicModel(mDynamicModelFile, false);
   mDynamicModelFile << "end" << endl; // Close *_dynamic function
-  writePowerDeriv(mDynamicModelFile, false);
   mDynamicModelFile.close();
 }
 
@@ -2022,7 +2020,6 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri
   mDynamicModelFile << "  oo_.endo_simul = y';\n";
   mDynamicModelFile << "return;\n";
   mDynamicModelFile << "end" << endl;
-  writePowerDeriv(mDynamicModelFile, false);
 
   mDynamicModelFile.close();
 
@@ -3674,9 +3671,6 @@ DynamicModel::writeParamsDerivativesFile(const string &basename) const
 
   paramsDerivsFile << "end" << endl
                    << "end" << endl;
-
-  writePowerDeriv(paramsDerivsFile, false);
-
   paramsDerivsFile.close();
 }
 
diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc
index 0fe9a37901f1a368083ba443c203818ab95a0a2a..c81f21918d5a2e0227b61015b0b7714b3295ba69 100644
--- a/preprocessor/StaticModel.cc
+++ b/preprocessor/StaticModel.cc
@@ -396,7 +396,6 @@ StaticModel::writeModelEquationsOrdered_M(const string &static_basename) const
           break;
         }
       output << "end" << endl;
-      writePowerDeriv(output, false);
       output.close();
     }
 }
@@ -1238,7 +1237,6 @@ StaticModel::writeStaticMFile(const string &func_name) const
 
   output << "end" << endl; // Close the if nargout >= 3 statement
   output << "end" << endl; // Close the *_static function
-  writePowerDeriv(output, false);
   output.close();
 }
 
@@ -1322,7 +1320,6 @@ StaticModel::writeStaticBlockMFSFile(const string &basename) const
     }
   output << "  end" << endl
          << "end" << endl;
-  writePowerDeriv(output, false);
   output.close();
 }