找到给出函数特定值的 x

问题描述 投票:0回答:1

我有这个功能:

a <- 1
b <- 2 
get_y <- function (x,a,b) {
   a * b * x
}

我想创建一个函数,它接受 get_y 并返回使 y = 4 的 x 。 我该怎么做?

r function solver uniroot
1个回答
1
投票

你可以解决

get_y(x,a,b) - 4 == 0

uniroot
。您不必创建新函数,匿名函数即可完成。

a <- 1
b <- 2 
get_y <- function (x,a,b) {
  a * b * x
}

uniroot(\(x) get_y(x,a,b) - 4, c(-10, 10))
#> $root
#> [1] 2
#> 
#> $f.root
#> [1] 0
#> 
#> $iter
#> [1] 1
#> 
#> $init.it
#> [1] NA
#> 
#> $estim.prec
#> [1] 12

创建于 2023-08-15,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.