當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。