R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # chex84.r > # Example 8.4 -- zig-zag behavior of steepest descent search > # > ########################## > # create function to be minimized (& gradient) > mkqfun <- function(A,b,c) { + list( f = function(x){ t(x)%*%A%*%x+t(b)%*%x+c}, # function + gr = function(x) as.vector( 2*A%*%x + b ) ) } # gradient > # > # create functions > qfun <- mkqfun(matrix(c(1,0,0,.5),2,2),c(0,0),0) > # > # start here and then loop > xcur <- c(1,2) > x1s <- 1 > x2s <- 1 > fs <- qfun$f(xcur) > fs [,1] [1,] 3 > qfun$gr(xcur) [1] 2 2 > # start loop > for (i in(1:10) ) { + grad <- qfun$gr(xcur) # gradient at current + lfun <- function(u) qfun$f(xcur-u*grad) # function on line + ym1 <- lfun(-1) + y0 <- lfun(0) + yp1 <- lfun(+1) + ustar <- - (yp1-ym1)/(2*(yp1-2*y0+ym1)) # best point on line + xcur <- xcur - ustar*grad # new point + x1s <- c(x1s,xcur[1]) + x2s <- c(x2s,xcur[2]) + fs <- c(fs,qfun$f(xcur)) } # new f & end loop > cbind(x1s,x2s,fs) x1s x2s fs [1,] 1.000000e+00 1.000000e+00 3.000000e+00 [2,] -3.333333e-01 6.666667e-01 3.333333e-01 [3,] 1.111111e-01 2.222222e-01 3.703704e-02 [4,] -3.703704e-02 7.407407e-02 4.115226e-03 [5,] 1.234568e-02 2.469136e-02 4.572474e-04 [6,] -4.115226e-03 8.230453e-03 5.080526e-05 [7,] 1.371742e-03 2.743484e-03 5.645029e-06 [8,] -4.572474e-04 9.144947e-04 6.272255e-07 [9,] 1.524158e-04 3.048316e-04 6.969172e-08 [10,] -5.080526e-05 1.016105e-04 7.743524e-09 [11,] 1.693509e-05 3.387018e-05 8.603916e-10 > # done > rm(list=ls()) > q()