[R] uniroot problem
Troels Ring
tring @ending from gvdnet@dk
Wed Dec 19 10:03:09 CET 2018
Dear friends and helpers - in the script below, uniroot is called with a
function CHB that calls a function, Charge. On its own, CHB apparently does
what is expected, but from within uniroot, problems appear. An error is
thrown
Error in f(lower, ...) : could not find function "f"
So CHB is not seen or understood from within uniroot?
I'm on windows 10, 64 bit R version 3.5.1 (2018-07-02)
All best wishes
Troels
kw <- 1e-14
TOT <- 1
Pk1 <- 10^-2.16
Pk2 <- 10^-7.21
Pk3 <- 10^-12.32
K <- c(Pk1,Pk2,Pk3)
f <- c(1,1,1)
H <- 10^-7.4
Charge <- function(TOT,f,K,H)
{
num <- c()
num[1] <- K[1]/(f[1]^2*H)
for (i in 2:length(K)) num[i] <- i*prod(K[1:i])/(f[1]^i*f[i]*H^i)
num <- sum(num)
denum <- c()
denum[1] <- 1+ K[1]/(f[1]^2*H)
for (i in 2:length(K)) denum[i] <- prod(K[1:i])/(f[1]^i*f[i]*H^i)
denum <- sum(denum)
num/denum
}
Na <- 0.140
Cl <- 0.1
CHB <- function(Na,Cl,H,K,f,TOT) {Na-Cl+H-kw/(f[1]^2*H)-Charge(TOT,f,K,H)}
H <- uniroot(CHB,interval=c(1e-19,5),tol=.Machine$double.eps,maxiter=100000,
Na=Na,Cl=Cl,K=K,TOT=TOT,f=f)$root
#Error in f(lower, ...) : could not find function "f"
CHB(Na,Cl,10^-7.4,K,f,TOT) # -1.567668 OK right!
[[alternative HTML version deleted]]
More information about the R-help
mailing list