constant <- function(x){1}
integrate(constant,lower=1,upper=10)
this is probably more than ridiculously simple, but any reason as to why this isn't working?
is there a different way I should be writing the function for constants?
You can use the Vectorize function to convert a non-vectorized function into the sort of function that integrate requires:
> constant <- function(x){1}
> Vconst <- Vectorize(constant)
> integrate(Vconst,lower=1,upper=10)
9 with absolute error < 1e-13
From ?integrate:
An R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.
You need to return a vector the same length as x. Try:
constant <- function(x){rep(1,length(x))}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With