title: Optimization of a function in R ( L-BFGS-B needs finite values of 'fn')
body: <p>I would like to optimize (maximum) of the following function <code>f1</code>. I wrote the following code which using the lower and upper bound, since  we know that all of our parameters are equal or bigger than zero and also we always should have <code>x4</code> values less than or equal <code>x6</code>. How can I fix this problem in R?I want to get a finite maximum value of function <code>f1</code>.</p>
  
  <pre><code>          x1 = 0.1
            x2 = 0.1
            x3 = 2
            x4 = 10
            x5 = 2
            x6 = 30
            x7 = 1
            par = list(x1=x1, x2=x2, x3=x3, x4=x4,x5=x5, x6=x6, x7=x7)
            par1 = c(1, 1, 2, 1.5, 1, 1.5, 1)
  
            f1 = function(x, par){
                sum(log(exp(-(par$x7)*(par$x1*x + par$x2*x^2/2 + 
                  par$x3 * (par$x4-x)^3/3+par$x5 *(x-par$x6)^3/3))))
            }
  
           x = seq(0, 500, length=100)
           z = c(par$x1, par$x2, par$x3, par$x4, par$x5, par$x6, par$x7)
  
          f2 = function(z){
              par.new = list(x1 = z[1], x2 = z[2], x3 = z[3], x4 = z[4]
             , x5 = z[5], x6 = z[6], x7 = z[7])
             f1(x, par.new)
          }
  
        optim(par1, f2, method = "L-BFGS-B", lower = rep(0, length(z)),
        upper = rep(Inf,length(z)),control = list(trace = 5,fnscale=-1)) 
        &gt; optim(par1, f2, method = "L-BFGS-B", lower = rep(0, length(z)), 
          upper = rep(Inf, length(z)), control = list(trace = 5,fnscale=-1))
          N = 7, M = 5 machine precision = 2.22045e-16
          L = 0 0 0 0 0 0 0 
         X0 = 1 1 2 1.5 1 1.5 1 
         U = inf inf inf inf inf inf inf 
         At X0, 0 variables are exactly at the bounds
         Error in optim(par1, f2, method = "L-BFGS-B", lower = rep(0, length(z)),  : 
         L-BFGS-B needs finite values of 'fn'
  </code></pre>