# 待完善 | R语言 | 优化函数 | optimize,optimise,optim

R语言中，常用的优化函数知多少，这次将介绍optimize,optimise,optim这三个做优化的函数，也是目前最常用到的优化函数。

```optimize(f, interval, ..., lower = min(interval), upper = max(interval),
maximum = FALSE,
tol = .Machine\$double.eps^0.25)
optimise(f, interval, ..., lower = min(interval), upper = max(interval),
maximum = FALSE,
tol = .Machine\$double.eps^0.25)```

=====================

`The first evaluation of f is always at x_1 = a + (1-φ)(b-a) where (a,b) = (lower, upper) and phi = (sqrt(5) - 1)/2 = 0.61803.. is the golden section ratio. Almost always, the second evaluation is at x_2 = a + phi(b-a). Note that a local minimum inside [x_1,x_2] will be found as solution, even when f is constant in there, see the last example.`

=====================

```f <- function (x) (x - 1/3)^2
plot(f,xlim = c(0,1))```
```xmin <- optimize(f, c(0, 1), tol = 0.0001)
xmin```

```\$minimum
[1] 0.3333333

\$objective
[1]0```

```f=function(x) x*sin(10*pi*x)+1
curve(f,xlim=c(-2,1))```

`optimize(f,c(-2,1),tol=0.0001,maximum=T)`

```\$maximum
[1] -1.050968

\$objective
[1] 2.050482```

```optim(par, fn, gr = NULL, ...,
method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN",
"Brent"),
lower = -Inf, upper = Inf,
control = list(), hessian = FALSE)

optimHess(par, fn, gr = NULL, ..., control = list())```

```require(graphics)

fr <- function(x) {   ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}```
`optim(c(-1.2,1), fr)`

```\$par
[1] 1.000260 1.000506

\$value
[1] 8.825241e-08

\$counts
195       NA

\$convergence
[1] 0

\$message
NULL```

• optimize
• optimise
• optim
• optimHess

176 篇文章40 人订阅

0 条评论