R语言 计算一个区间内方程的根 – uniroot()函数
R语言中的 uniroot() 函数用于计算一个方程的根,其参数为区间的下限和上限。
语法: uniroot(fun, interval, lower, upper)
参数:
fun: 带有方程的函数
interval: 带有根的上下范围
lower: 区间的下端点
upper: 区间的上端点
例1 :
# R program to calculate root of an equation
# Function with equation
fun <- function(x) {2 * x ^ 2 - 4 * x -10}
# Calling uniroot() function
uniroot(fun, lower = 0, upper = 4)
输出
$root
[1] 3.449485
$f.root
[1] -4.310493e-05
$iter
[1] 5
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
例2 :
# R program to calculate root of an equation
# Function with equation
fun <- function(x) {2 * x ^ 2 - 4 * x -10}
# Calling uniroot() function
uniroot(fun, c(0, 4))root
uniroot(fun, lower = -4, upper = 0)root
输出
[1] 3.449485
[1] -1.44949