Hilary H
Hilary H

Reputation: 11

Gradient Descent Method with Backtracking Function in R

I am trying to write a function in R to implement the gradient method with backtracking for a quadratic minimization problem min{x^T*A*x: x in R5} and A being a Hilbert matrix. The output of the function should be the number of iterations and the solution to x. Photo of the problem here

A <- hilbert.matrix(5)

dec_gradient <- function(f, g, x_0, s, alpha, beta, epsilon) {
x <- x_0
grad <- g(x)
fun_val <- f(x) 
iter <- 0

while(norm(grad) > epsilon) {
  iter <- iter + 1 
  t <- s
while (fun_val-f(x-t*grad)<alpha*t*norm(grad)^2) {
  t <- beta*t
  x <- x-t*grad
  fun_val <- f(x)
  grad <- g(x)
  print('iter_number = '+ str(iter) + ' norm_grad = ' + str(norm(grad)) + ' fun_val = ' + str(fun_val))
}
  return(x, fun_val)
}
}

f<- t(x)%*%A%*%x
g <- 2*A%*%x
alpha <- 0.5
beta <- 0.5
s <- 1
epsilon <- 10e-4
# define starting point
x_0 <- matrix(c(1,2,3,4,5), ncol = 1)

dec_gradient(f, g, x_0, s, alpha, beta, epsilon)

I keep getting the error

Error in g(x) : could not find function "g"

Upvotes: 0

Views: 381

Answers (1)

Hilary H
Hilary H

Reputation: 11

I was able to (with help) write the function that worked:


dec_gradient <- function(f, g, x_0, s, alpha, beta, epsilon) {
x <- x_0
grad <- g(x)
fun_val <- f(x) 
iter <- 0

while(norm(grad) > epsilon) {
  iter <- iter + 1 
  t <- s
while ((fun_val-f(x-t*grad))<alpha*t*norm(grad)^2) {
  t <- beta*t
  x <- x-t*grad
  fun_val <- f(x)
  grad <- g(x)
} 
  cat(paste0('iter_number = ', iter, "\n",  'norm_grad = ', norm(grad), "\n", 'fun_val = ', fun_val, "\n"))
  }
print(iter)
return(list(x, fun_val)) }


f <- function(x, .A=A) t(x) %*% .A %*% x
g <- function(x, .A=A) 2* .A %*% x
alpha <- 0.5
beta <- 0.5
s <- 1
epsilon <- 10e-4
# define starting point
x_0 <- matrix(c(1,2,3,4,5), ncol = 1)

dec_gradient(f, g, x_0, s, alpha, beta, epsilon) ```

Upvotes: 1

Related Questions