功能在哪里等于某个值

时间:2017-05-02 14:46:13

标签: r function

我为我的数据安装了一个函数:

BCF.plot <- function(x) {
  vv[2] +((vv[3]/(2*(1-vv[4])))*(cos(x-vv[1])-vv[4]+abs(cos(x-vv[1])-vv[4])))
}

它是基线(b)余弦波,即在其上面具有余弦波的基线。现在我在Y轴上有一定的值(dlmo_val),我想知道哪个x值对应于它。我尝试过这样的事情:

BCF.dlmo <- function(x, dlmo_val = 0) {
  vv[2] +((vv[3]/(2*(1-vv[4])))*(cos(x-vv[1])-vv[4]+abs(cos(x-vv[1])-vv[4])))-b-dlmo_val ## find point where function minus baseline & dlmo_val is 0
}

vv = c(2.3971780, 2.0666526, 11.1775231, 0.7870128)
b = 2.066653
H = 11.17752

dlmo_val = 0.4*H ## dlmo*peak height above baseline, H is result from optimisation
uniroot(BCF.dlmo, c(0.2617994, 6.021386), dlmo_val=dlmo_val)  ## lower & upper are min(x) and max(x)

然而,uniroot告诉我

"...values at end points not of opposite sign"

我不确定如何解决这个问题。任何建议都非常欢迎!

1 个答案:

答案 0 :(得分:1)

As described in this postuniroot()用于在函数中仅查找一个零,而您有两个零。您可以在较小的时间间隔内调用它:

uniroot(BCF.dlmo, c(0.2617994, 2.5), dlmo_val = dlmo_val)$root
# [1] 1.886079

正如该帖子所描述的那样,您可以使用rootSolve package中的unitroot.all函数来查找零:

library(rootSolve)
uniroot.all(BCF.dlmo, c(0.2617994, 6.021386), dlmo_val = dlmo_val)
# [1] 1.886084 2.908276