Ex2 Explore optimize() in R and try to solve the previous
problem.
minimum <- optimize(f, interval = c(-10, 10), maximum = FALSE)
print(minimum$minimum)
## [1] 0.9999986
Ex 3 Use any optimation algorithm to find the minimum of \(f(x,y) = ((x-1)^2 +100(y-x^2)^2\)
g <- function(x, y) {
result <- (x - 1)^2 + 100 * (y - x^2)^2
return(result)
}
find_minimum <- function() {
minimum <- optimize(g, interval = c(-10, 10), maximum = FALSE, y = 0)
return(minimum$minimum)
}
minimum <- find_minimum()
print(minimum)
## [1] 0.1612531
Explore the optimr package for R and try to solve the previous
problem.
g <- function(x) {
result <- (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2
return(result)
}
find_minimum <- function() {
result <- optim(par = c(0, 0), fn = g, method = "L-BFGS-B", lower = c(-10, -10), upper = c(10, 10))
return(result$par)
}
minimum <- find_minimum()
print(minimum)
## [1] 0.9998008 0.9996016