Reputation: 527
I have fitted a function to my data:
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])))
}
It is a baseline (b) cosine wave, i.e. a baseline with a cosine wave on top of it. Now I have a certain value on the Y-axis (dlmo_val) and I want to know which x value corresponds to it. I have tried something like this:
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)
However, uniroot tells me
"...values at end points not of opposite sign"
I am not really sure how to go about this. Any recommendations are more than welcome!
Upvotes: 1
Views: 183
Reputation: 78660
As described in this post, uniroot()
is designed for finding only one zero in a function, while you have two zeroes. You could call it on a smaller interval:
uniroot(BCF.dlmo, c(0.2617994, 2.5), dlmo_val = dlmo_val)$root
# [1] 1.886079
As that post describes, you can instead use the unitroot.all
function in the rootSolve package to find both zeroes:
library(rootSolve)
uniroot.all(BCF.dlmo, c(0.2617994, 6.021386), dlmo_val = dlmo_val)
# [1] 1.886084 2.908276
Upvotes: 1