Error with curve (): 'expr' was not evaluated by an object of length 'n'

I have a function that has a vector in the numerator and the sum of the vector in the denominator. Thus, each component is one component of the vector divided by a function that depends on the sum of the components. I want to build a curve representing a function in the first component fixing other components. Why is this code not working?

y <- vector(length=2)
y0 <- c(1,2)
f <- function(y) y/(1+sum(y))
g <- function(x) f(c(x,y0[2]))[1]
curve(g, 0, 2)

Both f and g evaluate, as expected, at numerical values, but the curve function gives an error:

Error in curve(g, 0, 2) : 
  'expr' did not evaluate to an object of length 'n'

I tried Vectorizing g with no success. Appreciate any help.

+4
source share
1 answer

You can try:

h <- Vectorize(g); curve(h, 0, 2)

enter image description here

+5
source

All Articles