Pedro Cavalcante
Pedro Cavalcante

Reputation: 444

Function that takes in a real number and outputs a function with it as a parameter

I want to code a function that takes in a number and returns a function with that number as a parameter. I want to later find the roots of this function it outputs.

fooToOptimze <- function(PD) {

  f <- function(C, k) { 0.089 * exp(1.6234 * PD) - C * exp(k * PD)}

  return(f)

}

However, this way, I get:

fooToOptimize(.5)

OUTPUT 

function(C, k) { 0.089 * exp(1.6234 * PD) - C * exp(k * PD)}

How should I approach this?

Upvotes: 0

Views: 73

Answers (1)

slava-kohut
slava-kohut

Reputation: 4233

You don't have to use purrr for this. Your function seems to look OK. Again, suppose you want to create a function that takes a and b as parameters and returns a function for evaluating ax+b. Here is what you do:

lin <- function(a,b){
  function(x) a*x + b
}

Then lin(1,1)(1) will give you 1*1 + 1 = 2. lin(1,0)(x) will just give you the input value back for any x.

Upvotes: 1

Related Questions