当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 optimize()用法及代码示例


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

用法: optimize(f, interval, maximum)
参数: 
f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the value of maximum.
interval: a vector containing the end-points of the interval to be searched for the minimum.
maximum: the logical value says to maximize or minimize. Its default value is minimize. 
 

范例1:

Python3


# R program to illustrate
# optimize function
 
# Specifying a funtion
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))

输出:



$minimum
[1] 1.2

$objective
[1] 9.8

范例2:

Python3


# R program to illustrate
# optimize function
 
# Specifying a funtion
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

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Search the Interval for Minimum and Maximum of the Function in R Programming – optimize() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。