📜  在 R 编程中搜索函数的最小值和最大值的区间 – optimize()函数

📅  最后修改于: 2022-05-13 01:55:51.822000             🧑  作者: Mango

在 R 编程中搜索函数的最小值和最大值的区间 – optimize()函数

R 语言中的optimize() 或 optimise()函数用于在从下到上的区间中搜索函数f相对于其第一个参数的最小值或最大值。

示例 1:

Python3
# R program to illustrate
# optimize function
 
# Specifying a function
f <- function(x) {5 * x ^ 2 - 12 * x + 17}
 
# Calling the optimize() function
# over the interval of -5 to 5, to
# minimize the value
optimize(f, interval = c(-5, 5))


Python3
# R program to illustrate
# optimize function
 
# Specifying a function
f <- function(x) {5 * x ^ 2 - 12 * x + 17}
 
# Calling the optimize() function
# over the interval of -5 to 5, to
# maximize the value
optimize(f, interval = c(-5, 5), maximum = T)


输出:

$minimum
[1] 1.2

$objective
[1] 9.8

示例 2:

Python3

# R program to illustrate
# optimize function
 
# Specifying a function
f <- function(x) {5 * x ^ 2 - 12 * x + 17}
 
# Calling the optimize() function
# over the interval of -5 to 5, to
# maximize the value
optimize(f, interval = c(-5, 5), maximum = T)

输出:

$maximum
[1] -4.999944

$objective
[1] 201.9965