uniroot problem

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

uniroot problem

tring
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: uniroot problem

tring
Thanks a lot - f was renamed FF and things are OK
BW
Troels

-----Oprindelig meddelelse-----
Fra: Berwin A Turlach <[hidden email]>
Sendt: 19. december 2018 10:27
Til: Troels Ring <[hidden email]>
Emne: Re: [R] uniroot problem

G'day Troels,

On Wed, 19 Dec 2018 10:03:09 +0100
"Troels Ring" <[hidden email]> wrote:

> 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?

Read the help page of uniroot.  

The first argument is called "f", it is the function for which the root is
searched.  In your call:

> uniroot(CHB,interval=c(1e-19,5),tol=.Machine$double.eps,maxiter=100000,
>              Na=Na,Cl=Cl,K=K,TOT=TOT,f=f)$root

You pass the object "f" (a vector "f <- c(1,1,1)" created earlier in your
code) to the argument "f".  Presumably there is no function called "f" in
your search path, and so R correctly complains that it cannot find the
function whose root you are looking for.

In R, arguments are passed first by exact matching of actual and formal
arguments, then by partial matching and then by position.

The easiest fix is probably to rename the object "f" to something else.

Cheers,

        Berwin

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.