R Under development (unstable) (2023-10-23 r85401 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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. 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. > # maxfn.R > ## author: John C. Nash > > # solution: 1:n has maximum of 10. > > maxfn<-function(x) {# fn to be MAXIMIZED + # max = 10 at 1:n + n<-length(x) + ss<-seq(1,n) + f<-10-sum((x-ss)^2) + f + } > > maxfn.g <- function(x) { # gradient + n <- length(x) + ss<-seq(1,n) + gg <- -2*(x-ss) + gg + } > > maxfn.h <- function(x) { # gradient + n <- length(x) + hh<-rep(-2, n) + hh <- diag(hh) + hh + } > > # solution: 1:n has minimum of -10. > > negmaxfn<-function(x) {# explicit negative of maxfn + f<-(-1)*maxfn(x) + return(f) + } > negmaxfn.g<-function(x) {# explicit negative of maxfn + gg<-(-1)*maxfn.g(x) + gg + } > negmaxfn.h<-function(x) {# explicit negative of maxfn + hh<-(-1)*maxfn.h(x) + hh + } > > > proc.time() user system elapsed 0.10 0.03 0.12