title: calculating the Gradient and the Hessian in R
body: <p>As you know, the Gradient of a function is the following vector:</p>
  
  <p><img src="https://i.stack.imgur.com/DUwU0.png" alt="the Gradient"></p>
  
  <p>and the Hessian is the following matrix:</p>
  
  <p><img src="https://i.stack.imgur.com/3LJoo.png" alt="the Hessian"></p>
  
  <p>Now, I wonder, is there any way to calculate these in R for a user defined function at a given point?</p>
  
  <p>First, I've found a package named <a href="http://cran.r-project.org/package=numDeriv" rel="nofollow noreferrer"><code>numDeriv</code></a>, which seems to have the necessary functions <code>grad</code> and <code>hessian</code> but now I can't get the correct results... Thus, here's my workflow:</p>
  
  <p>Let's say that we are given the function f(x,y) = x^2 * x^3, and we need to calculate the Gradient and the Hessian at the point (x=1, y=2).</p>
  
  <p>That's been said, I define this function within R:</p>
  
  <pre><code>dummy &lt;- function(x,y) {
    rez &lt;- (z^2)*(y^3)
    rez
  }
  </code></pre>
  
  <p>and then use <a href="http://artax.karlin.mff.cuni.cz/r-help/library/numDeriv/html/grad.html" rel="nofollow noreferrer"><code>grad</code></a> the following way:</p>
  
  <pre><code>grad(func=dummy, x=1, y=2)
  </code></pre>
  
  <p>which gives me result 16 -- and the problem is that this only the first value from a gradient vector, the correct version of which is </p>
  
  <pre><code>[16, 12]
  </code></pre>
  
  <p>Same goes with the <a href="http://artax.karlin.mff.cuni.cz/r-help/library/numDeriv/html/hessian.html" rel="nofollow noreferrer"><code>hessian</code></a>:</p>
  
  <pre><code>hessian(func=dummy, x=1, y=2)
  </code></pre>
  
  <p>which gives my 1x1 matrix with the value 16 instead of the 2x2 matrix</p>
  
  <pre><code>     [,1] [,2]
  [1,]   16   24
  [2,]   24   12
  </code></pre>
  
  <p>So, the question is what am I doing wrong?</p>
  
  <p>Thank you.</p>