Dear All, I would like to define a function: f(x,y,z) with three arguments x,y,z, such that: given values for x,y, f(x,y,z) is still a function of z and that I am still allowed to find the root in terms of z when x,y are given. For example: f(x,y,z) = x+y + (x^2-z), given x=1,y=3, f(1,3,z)= 1+3+1-z is a function of z, and then I can use R to find the root z=5. Thank you. -Chee [[alternative HTML version deleted]]
On Thu, 2011-04-28 at 23:08 -0400, Chee Chen wrote:> Dear All, > I would like to define a function: f(x,y,z) with three arguments x,y,z, such that: given values for x,y, f(x,y,z) is still a function of z and that I am still allowed to find the root in terms of z when x,y are given. > For example: f(x,y,z) = x+y + (x^2-z), given x=1,y=3, f(1,3,z)= 1+3+1-z is a function of z, and then I can use R to find the root z=5. > > Thank you. > -CheeInteresting exercise. I've got this function, which I think it's doing what you're asking. f <- function(x,y,z) { fcall <- match.call() fargs <- NULL if(fcall$x == "x") fargs <- c(fargs, "x") if(fcall$y == "y") fargs <- c(fargs, "y") if(fcall$z == "z") fargs <- c(fargs, "z") ffunargs <- as.list(fargs) names(ffunargs) <- fargs argslist <- list(fcall) ffun <- append(argslist, substitute( x+y + (x^2-z) ), after=0)[[1]] as.function(append(ffunargs, ffun)) } This yields.> f(3, 2, z)function (z = "z") 3 + 2 + (3^2 - z) <environment: 0x132fdb8>> f(3, 2, z)(3)[1] 11 I haven't figured out how to get rid of the default argument value shown here as 'z = "z"'. That doesn't prevent it to work, but it's less pretty. If you find a better way, let me know. HTH, Jerome
Here's one possibility:> funmaker = function(x,y,z)function(z)x + y + (x^2 - z) > uniroot(funmaker(1,3,z),c(0,10))$root[1] 5> uniroot(funmaker(5,2,z),c(30,40))$root[1] 32 (The third argument to the function doesn't really do anything.) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 2011-04-28 at 23:08 -0400, Chee Chen wrote:> Dear All, > I would like to define a function: f(x,y,z) with three arguments x,y,z, such that: givenvalues for x,y, f(x,y,z) is still a function of z and that I am still allowed to find the root in terms of z when x,y are given.> For example: f(x,y,z) = x+y + (x^2-z), given x=1,y=3, f(1,3,z)= 1+3+1-z is a function ofz, and then I can use R to find the root z=5.>
On Thu, Apr 28, 2011 at 11:08:23PM -0400, Chee Chen wrote:> Dear All, > I would like to define a function: f(x,y,z) with three arguments x,y,z, such that: given values for x,y, f(x,y,z) is still a function of z and that I am still allowed to find the root in terms of z when x,y are given. > For example: f(x,y,z) = x+y + (x^2-z), given x=1,y=3, f(1,3,z)= 1+3+1-z is a function of z, and then I can use R to find the root z=5.If solving the equation for z with given x and y is the main purpose, then try the following f <- function(x,y,z) x+y + (x^2-z) uniroot(f, c(0, 10), x=1, y=3)$root [1] 5 Hope this helps. Petr Savicky.
Maybe Matching Threads
- Getting the argument list within a function
- purrr::pmap does not work
- [LLVMdev] Problem about the type of Function's arguement in llvm
- [LLVMdev] Problem about the type of Function's arguement in llvm
- [LLVMdev] Problem about the type of Function's arguement in llvm