gdls <- function ( A, b, alpha = 0.005, tol = 1.0e-6, m = 1.0e5 ) #*****************************************************************************80 # ## gdls solves a least squares problem using gradient descent. # # 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: # # 20 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. # { source ( "/home/burkardt/public_html/r_src/vecnorm/vecnorm.R" ) iter <- 0 n <- ncol ( A ) theta <- matrix ( rep ( 0, n ) ) oldtheta <- theta + 10.0 * tol while ( TRUE ) { iter <- iter + 1 if ( m < iter ) { warning ( 'gdls: iteration limit exceeded.' ) return ( theta ) } e <- ( A %*% theta - b ) d <- ( t(A) %*% e ) oldtheta <- theta theta <- theta - alpha * d if ( vecnorm ( theta - oldtheta ) < tol ) { break } } cat ( ' number of iterations', iter, '\n' ) return ( theta ) }