35.2 一元非线性优化

复合函数求极值

\[ g(x) = \int_{0}^{x} -\sqrt{t}\exp(-t^2) dt, \\ f(y) = \int_{0}^{y} g(s) \exp(-s) ds \]

g <- function(x) {
  integrate(function(t) {
    -sqrt(t) * exp(-t^2)
  }, lower = 0, upper = x)$value
}

f <- function(y) {
  integrate(function(s) {
    Vectorize(g, "x")(s) * exp(-s)
  }, lower = 0, upper = y)$value
}

optimize(f, interval = c(10, 100), maximum = FALSE)
## $minimum
## [1] 66.84459
## 
## $objective
## [1] -0.3201572

计算积分的时候,输入了一系列 s 值,参数是向量,而函数 g 只支持输入参数是单个值,g(c(1,2)) 则会报错

g(1)
## [1] -0.453392