title: Error in optim: function cannot be evaluated at initial parameters
body: <p>So I've run into this weird error in R. I have a simple function which returns an error term when comparing real and simulated prices, called <code>hestondifferences()</code>.</p>
  
  <p>when I try to find the local minima via:</p>
  
  <pre><code> res&lt;-optim(fn=hestondifferences, par = c(vT=vT, rho=rho, k=k, sigma=sigma))
  </code></pre>
  
  <p>I get the error message: </p>
  
  <blockquote>
    <p>Error in optim(fn = hestondifferences, par = c(vT = vT, rho = rho, k =
    k,  : function cannot be evaluated at initial parameters</p>
  </blockquote>
  
  <p>What confuses me is that calling the function directly with the initial parameters <code>hestondifferences(vT, rho, k, sigma)</code> returns the correct value.</p>
  
  <p>The function <code>hestondifferences()</code> is written in a way that whenever the simulation is impossible for a certain set of parameters, it returns <code>NA</code> which is in line with what <code>optim</code>() expects.</p>