library(animation)
# gradient descent works
saveHTML({ani.options(nmax = 50, interval = 0.3)
grad.desc()
},
img.name="grad_desc_a", htmlfile="grad_desc_a.html",
ani.height = 500, ani.width = 500, outdir = getwd(),
title = "Demonstration of the Gradient Descent Algorithm",
description = c("The arrows will take you to the optimum step by step."))
# gradient descent fails
saveHTML({ani.options(nmax = 70, interval = 0.2)
f2 = function(x, y) sin(1/2 * x^2 - 1/4 * y^2 + 3) *
cos(2 * x + 1 - exp(y))
grad.desc(f2, c(-2, -2, 2, 2), c(-1, 0.5), gamma = 0.3, tol = 1e-04)
},
img.name="grad_desc_b", htmlfile="grad_desc_b.html",
ani.height = 480, ani.width = 480, outdir = getwd(),
title = "When Gradient Descent Algorithm Fails",
description = c("This example shows how the gradient descent algorithm will fail with
a too large step length."))