Advertisement
celestialgod

Rcpp call R optim (R code part)

Oct 21st, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.40 KB | None | 0 0
  1. library(Rcpp)
  2. sourceCpp("optim_cpp.cpp")
  3. fr <- function(x) {   ## Rosenbrock Banana function
  4.   x1 <- x[1]
  5.   x2 <- x[2]
  6.   100 * (x2 - x1 * x1)^2 + (1 - x1)^2
  7. }
  8. grr <- function(x) { ## Gradient of 'fr'
  9.   x1 <- x[1]
  10.   x2 <- x[2]
  11.   c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
  12.     200 *      (x2 - x1 * x1))
  13. }
  14. optim_cpp(fr, grr, c(-1.2,1), method = "BFGS")
  15.  
  16. optim(c(-1.2,1), fr, grr, method = "BFGS")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement