I have this integration that includes a function. I want to integrate over x values 0-39.
#parameters
alpha <- 0.86
y <- 70000
t <- 600
u <- 5353.65
get_p <- function(x,y,t,u) {
(((alpha^alpha) * ((1 - alpha)^(1 - alpha)) * (y - t * x)) / u)^(1 / (1 - alpha))
}
integrate(x * get_p(x,y,t,u),lower = 0,upper = 39)
But when I run this code I get the error if I first put x <- 0:
Error in match.fun(f) :
'x * get_p(x, y, t, u)' is not a function, character or symbol
And I get this error if I don't specify x first:
Error in match.fun(f) : object 'x' not found
x is not supposed to be defined, we are integrating over i!
What am I doing wrong?
The integrate()
function needs a proper function as the first parameter, not an expression. You can use an anonymous function with
integrate(function(x) {x * get_p(x,y,t,u)},lower = 0, upper = 39)
or with the latest lambda syntax
integrate(\(x) {x * get_p(x,y,t,u)},lower = 0, upper = 39)