OPTIMIZATION IN SPLUS AND R --------------------------- deriv and deriv3 ---------------- deriv( ~ cos(x*y), c("x","y")) deriv( ~ cos(x*y)/(x^2+y^2+1), c("x","y")) deriv( ~ cos(x*y)/(x^2+y^2+1), c("x","y"), c("x","y")) fn1 _ deriv( ~ cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) library(MASS) fn2 _ deriv3( ~ cos(x*y)/(x^2+y^2+1),c("x","y"),function(x=0,y=0) NULL) plotting -------- fn1 _ function(x,y) { cos(x*y)/(x^2+y^2+1) } x _ seq(-pi,pi,length=20) y _ x z _ outer(x,y,fn1) persp(x,y,z) using ms -------- library(MASS) fn2 _ function(x,y) { - cos(x*y)/(x^2+y^2+1) } ms( ~ fn2(x,y), start=c(x=-2,y=-2)) ms( ~ fn2(x,y), start=c(x=-1,y=-1)) ms( ~ fn2(x,y), start=c(x=-3,y=-3)) fn2 _ function(x,y) { - cos(x*y)/(x^2+y^2+1) } summary(ms( ~ fn2(x,y), start=c(x=-1,y=-1))) fn2 _ deriv( ~ - cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) summary(ms( ~ fn2(x,y), start=c(x=-1,y=-1))) fn2 _ deriv3( ~ - cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) summary(ms( ~ fn2(x,y), start=c(x=-1,y=-1))) using nlmin ----------- fn2 _ function(theta) {x _ theta[1]; y _ theta[2]; - cos(x*y)/(x^2+y^2+1) } nlmin(fn2,c(-1,-1)) using nlminb ------------ fn2 _ function(theta) {x _ theta[1]; y _ theta[2]; - cos(x*y)/(x^2+y^2+1) } fn3 _ deriv3( ~ - cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) fn2gh _ function(theta) { x _ theta[1]; y _ theta[2] result _ fn3(x,y) g _ attr(result,"gradient") H _ matrix(attr(result,"hessian"),ncol=2) list(gradient=g,hessian=H[row(H)<=col(H)]) } fn2g _ function(theta) { fn2gh(theta)$gradient } nlminb(c(-1,-1),fn2,lower=c(-5,-5),upper=c(4,4)) nlminb(c(-1,-1),fn2,gradient=fn2g,lower=c(-5,-5),upper=c(4,4)) nlminb(c(-1,-1),fn2,gradient=fn2gh,hessian=T,lower=c(-5,-5),upper=c(4,4)) using nlm in R -------------- fn2 _ function(theta) {x _ theta[1]; y _ theta[2]; - cos(x*y)/(x^2+y^2+1) } nlm(fn2,c(-1,-1),hessian=T) fn3 _ deriv3( ~ - cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) fn2 _ function(theta) { x _ theta[1]; y _ theta[2]; fn3(x,y) } nlm(fn2,c(-1,-1),hessian=T) using optim in R ---------------- fn3 _ deriv3( ~ - cos(x*y)/(x^2+y^2+1), c("x","y"), function(x=0,y=0) NULL) fn2 _ function(theta) { x _ theta[1]; y _ theta[2]; - cos(x*y)/(x^2+y^2+1) } fn2g _ function(theta) { x _ theta[1]; y _ theta[2]; attr(fn3(x,y), "gradient") } optim(c(-1,-1),fn2,fn2g,hessian=T) optim(c(-1,-1),fn2,hessian=T) ------------------------------------------------------------------------