I think it's more flexible to put a
into f1()
as an argument.
f1 <- function(x, a) ef(x, a)-0.75*ef(2.5, a)-0.25*ef(-1, a)
Then use sapply()
to operate each value in the sequence seq(0.05, 3, 0.05)
:
sapply(seq(0.05, 3, 0.05), function(A){
uniroot(f1, c(-10, 10), tol = 0.0001, extendInt = "yes", a = A)$root
})
# [1] 1.565924900 1.503659791 1.438426382 1.370549617 1.300423929
# [6] 1.228478774 1.155273229 1.081323809 1.007194271 0.933431003 ...
The argument extendInt = "yes"
can conquer the error when f1()
does not have different signs at the endpoints. In addition, I prefer apply
family rather than a for
loop in this case. You can read this for the reason.
Edit: for
loop solution
a <- seq(0.05, 3, 0.05)
root <- numeric()
for(i in 1:length(a)){
root[i] <- uniroot(f1, c(-10, 10), tol = 0.0001, extendInt = "yes", a = a[i])$root
}
At the end of the loop, the variable root
will store all the roots. You can check whether the outputs of the two solutions are equal.