题目:
迭代思路(最佳步长α的确定公式) :
代码展示:
# 最速下降法的实现
steepest_descent <- function(f, grad_f, x0, alpha=0.1, epsilon=1e-5, max_iter=3) {
x <- x0
H <- matrix(c(2,0,0,0,2,0,0,0,2), nrow=3, byrow=TRUE)
trajectory <- list(x0) # 记录迭代过程中的点
grad_list <- list(grad_f(x0)) #记录迭代过程中的梯度向量
for (i in 1:max_iter) {
gradient <- grad_f(x)
if (crossprod(gradient)^2 < epsilon) {
break
}
# 计算alpha
alpha_denominator <- t(gradient) %*% H %*% gradient
alpha_numerator <- t(gradient) %*% gradient
alpha <- alpha_numerator / alpha_denominator
x <- x - alpha * gradient
trajectory <- append(trajectory, list(x))
grad_list <- append(grad_list, list(gradient))
}
return(list(x=x, trajectory=trajectory, grad_list=grad_list))
}
# 示例函数和其梯度
f <- function(x) {
return(x[1]^2 + x[2]^2 + x[3]^2)
}
grad_f <- function(x) {
return(c(2*x[1], 2*x[2], 2*x[3]))
}
# 初始点
x0 <- c(2.0, -2.0, 1.0)
# 执行最速下降法
result <- steepest_descent(f, grad_f, x0)
solution <- result$x
trajectory <- do.call(rbind, result$trajectory)
grad_list <- do.call(rbind, result$grad_list)
print(solution)
print(trajectory)
print(grad_list)
运行结果:
如有错误,请批评指正,一起交流,感谢!