在 R 编程中计算一个区间内方程的根 – uniroot()函数
R 语言中的uniroot()
函数用于计算方程的根,其中区间的下限和上限作为参数传递。
Syntax: uniroot(fun, interval, lower, upper)
Parameters:
fun: function with the equation
interval: with upper and lower range of root
lower: lower end point of the interval
upper: upper end point of the interval
示例 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