goldsectmax <- function ( f, a, b, tol = 1.0e-3, m = 100 ) #*****************************************************************************80 # ## goldsectmax seeks the maximum of a function in [a,b] by golden section. # # Licensing: # # Copyright 2016 James P. Howard, II # # The computer code and data files on this web page are distributed under # https://opensource.org/licenses/BSD-2-Clause, the BSD-2-Clause license. # # Modified: # # 14 March 2020 # # Author: # # Original R code by James Howard; # Modifications by John Burkardt. # # Reference: # # James Howard, # Computational Methods for Numerical Analysis with R, # CRC Press, 2017, # ISBN13: 978-1-4987-2363-3. # { iter <- 0 phi <- ( sqrt ( 5.0 ) - 1.0 ) / 2.0 a.star <- b - phi * abs ( b - a ) b.star <- a + phi * abs ( b - a ) while ( tol < abs ( b - a ) ) { iter <- iter + 1 if ( m < iter ) { warning ( 'goldsectmax: iteration limit exceeded.' ) break } if ( f ( b.star ) < f ( a.star ) ) { b <- b.star b.star <- a.star a.star <- b - phi * abs ( b - a ) } else { a <- a.star a.star <- b.star b.star <- a + phi * abs ( b - a ) } } value <- 0.5 * ( a + b ) return ( value ) }