I am trying to do a descent gradient algorithm with a Himmelblau function in R. This is my code:
`library(numDeriv)
gradient_descent <- function(f, start_value, max_iter) {
gamma <- 1/max_iter #how much the function will move to the negative direction
#of the gradient function in order to have a minimum (the learning rate)
current_value <- start_value
for (i in 1:max_iter) {
grad_f <- grad(f, current_value)
step_size <- gamma * grad_f
current_value <- current_value - gamma * grad_f #minus for the negative
#direction (to get the minimum)
if (step_size <0.001){ #if the step size is smaller than 0.001
break
}
}
return(current_value)
}`
but I get this error: Error in if (step_size < 0.001) { : the condition has length > 1
Any idea what is happening?
Thanks!