title: How to Automatically Change a Local Variable within a Function in R?
body: <p>To simplify the question, I will use a toy example here. I want to derive the gradient and Hessian of a polynomial function such as
      library(pracma)</p>
  
  <pre><code>dummy &lt;- function(x) {
           z &lt;- x[1]; y &lt;- x[2]
           rez &lt;- (z^2)*(y^3)+3
           rez
           }
  grad(dummy, c(1,2))
  
  hessian(dummy, c(1,2))
  </code></pre>
  
  <p>M question is that is there an efficient way so I can derive the gradient and Hessian from different constant terms in <code>dummy</code>? e.g., the gradients and Hessians from <code>(z^2)*(y^3)+a</code> at <code>z=1</code> and <code>y=2</code>, where <code>a=[0.01,3]</code>. </p>
  
  <p>Thanks!</p>