#--------------------------------------------------------------------- # Minimizes a multivariable objective function # using steepest descent directions. Step length is # determined using the R function optimize() # # Usage: steepest(obj, gradient, hessian , x0, eps) # # Required Arguments: # obj(x): returns a value of the objective function evaluated # at a vector x # gradient(x): returns the derivative vector of the objective # function evaluated at a vector x # hessian(x): returns the hessian matrix of the objective function # evaluated at a vector x # x0: initial vector # eps: tolerance #----------------------------------------------------------------------- steepest=function(obj, gradient, hessian , x0, maxiter=200, eps=.001) { xi = x0 cat(" Starting Values: ", x0, fill = T) i=0 repeat { i = i + 1 cat("\n Current Function Value: ", obj(xi), fill = T) gi = gradient(xi) gi.star=gi/sqrt(sum(gi^2)) fi=function(a,x,g,fun) fun(x - a*g) alpha=optimize(fi,c(0,1),lower=0,upper=1,maximum=F,tol=.0001,xi,gi.star,obj)[[1]][1] if(i>100)break xinew = xi - alpha*gi.star if( sqrt((sum((xinew - xi)^2))) < eps)break xi=xinew cat("\n Estimates at Iteration ",i , ":", xi, fill = T) } if(i>maxiter) cat ("\n ****** No. of Iterations",maxiter," Exceeded: Terminated",fill = T) else cat("\n Convergence Criterion of ",eps, " met ", fill = T) cat("\n Final Estimates Are: ",fill = T) cat("\n ", signif(xinew,8), fill = T) cat("\n Final Function Value: ",fill = T) cat("\n ", signif(obj(xinew),12), fill = T) cat("\n Value of Gradient at Convergence:",fill = T) cat("\n ", signif(gradient(xinew),8) , fill = T) cat("\n Inverse of Negative Hessian",fill = T) cat(signif(solve(-1 *hessian(xinew)),8),fill = T) }