# tests/jonesrun.R In optimrx: Expanded Replacement and Extension of the 'optim' Function

```# test function from book by Owen Jones et al book

# function is 2 variables to be maximized
#  f(x,y)=sin(x^2/2 - y^2/4)*cos(2x-exp(y))

# ?? seems to have unstable parameter 1

jones<-function(xx){
x<-xx
y<-xx
ff<-sin(x*x/2 - y*y/4)*cos(2*x-exp(y))
ff<- -ff
}

jonesg <- function(xx) {
x<-xx
y<-xx
gx <-  cos(x * x/2 - y * y/4) * ((x + x)/2) * cos(2 * x - exp(y)) -
sin(x * x/2 - y * y/4) * (sin(2 * x - exp(y)) * 2)
gy <- sin(x * x/2 - y * y/4) * (sin(2 * x - exp(y)) * exp(y)) - cos(x *
x/2 - y * y/4) * ((y + y)/4) * cos(2 * x - exp(y))
gg <- - c(gx, gy)
}

tufn <- function (par) {
f <- jones(par)
g <- jonesg(par)
attr(f, "hessian") <- NULL
f
}

# require(Rtnmin)

# xx<-0.5*c(pi,pi)
# atn<-tn(xx, tufn)
# atn

# lo<-c(0,0)
# up<-c(1.2*pi, 1.2*pi)
# atnb<-tnbc(xx, tufn, lower=lo, upper=up)
# atnb

library(optimrx)

xx<-0.5*c(pi,pi)
ans<-opm(xx, jones, jonesg, method="ALL", control=list(trace=1))
print(summary(ans, order=value))

lo<-c(0,0)
up<-c(1.2*pi, 1.2*pi)
ansb<-opm(xx, jones, jonesg, lower=lo, upper=up, method="ALL", control=list(trace=1))
print(summary(ansb, order=value))

cat("Note: ansbnf shows failure for L-BFGS-B when trying to use forward diff approximation\n")

ansbnf<-opm(xx, jones, gr="grfwd", lower=lo, upper=up, method="ALL", control=list(trace=1))
print(summary(ansbnf, order=value))