## Minimizing a function f <- function(x) x^2 + 1 x <- -10:10 plot(x, f(x)) ## pdf('../figs/quad.pdf') ## plot(x, f(x)) ## dev.off() ## We can easily see that the minimum is at zero ## At the value x = 0 the function takes the value of 1 ## How do we use tools in R to minimize this function? ## Use the function optimize op <- optimize(f = f, lower = -20, upper = 20) ## Where is the maximum of this function? f2 <- function(x) 6 + x * 7.5 + x^2 * -0.6 x <- -20:20 plot(x, f2(x)) ## It is not easy to determine visually op2 <- optimize(f = f2, lower = -20, upper = 20, maximum = TRUE) ## Analytical method ## Derive and equate to zero df2 <- function(x) 7.5 + x * 2 *-0.6 ## Answer max.df2 <- -7.5/(2 * -0.6) # A third function f3 <- function(x) log(x) / (1 + x) ## This function implies that the domain set is x >= 0 ## There is no analytical solution to find the maximum xx <- seq(1, 5, 0.05) plot(xx, f3(xx)) df3 <- function(x) (1 + 1/x - log(x))/(x + 1)^2 plot(df3(xx) ~ xx) abline(h = 0) op3 <- optimize(f = f3, lower = 1, upper = 5, maximum = TRUE)