grad.desc(FUN = function(x, y) x^2 + 2 * y^2, rg = c(-3, -3, 3, 3),
init = c(-3, 3), gamma = 0.05, tol = 0.001, len = 50,
interact = FALSE, control = ani.control(interval = 0.1), ...)
c(x0, y0, x1, y1)
formani.control
ani.control
par
control$nmax
, it's quite likely that the solution is not reliable because the maximum number of iterations has been reachedderiv
persp
(see the examples below)nmax
in control
) has not been reached.deriv
, persp
, contour
, optim
# default example
xx = grad.desc()
xx$par # solution
xx$persp(col = "lightblue", phi = 30) # perspective plot
# define more complex functions; a little time-consuming
f1 = function(x, y) x^2 + 3 * sin(y)
xx = grad.desc(f1, pi * c(-2, -2, 2, 2), c(-2 * pi, 2))
xx$persp(col = "lightblue", theta = 30, phi = 30)
# or
f2 = function(x, y) sin(1/2 * x^2 - 1/4 * y^2 + 3) *
cos(2 * x + 1 - exp(y))
xx = grad.desc(f2, c(-2, -2, 2, 2), c(-1, 0.5),
gamma = 0.1, tol = 1e-04, nmax = 200, interval = 0)
# click your mouse to select a start point
xx = grad.desc(f2, c(-2, -2, 2, 2), interact = TRUE,
tol = 1e-04, nmax = 200, interval = 0)
xx$persp(col = "lightblue", theta = 30, phi = 30)
# HTML animation pages
ani.start()
grad.desc(saveANI = TRUE, width = 600, height = 600)
ani.stop()
Run the code above in your browser using DataLab