Reputation: 391
I am having this annoying error again in R that I thought I understood well and I could avoid it for enough time. But now it's starting to haunt me again.
inner.f.y <- function(y)
{
cat("length(y) ", length(y), "\n")
t<-2*y*exp((exp(-1*1i)-1)*y)
cat("length(t) ", length(t), "\n")
t
}
integrate(inner.f.y, lower = 0.01, upper = 8)
This is the output on the console
> integrate(inner.f.y, lower = 0.01, upper = 8)
length(y) 21
length(t) 21
Error in integrate(inner.f.y, lower = 0.01, upper = 8) :
evaluation of function gave a result of wrong type
So what can it be this issue ?
Upvotes: 1
Views: 1753
Reputation: 19857
See ?integrate
(my emphasis):
f : an R function taking a numeric first argument and returning a numeric vector of the same length.
Your function inner.f.y
does not return a numeric vector, but a complex one:
a <- inner.f.y(1)
is.numeric(a)
[1] FALSE
is.complex(a)
[1] TRUE
As @Ben Bolker suggests, you could use elliptic::myintegrate
instead:
elliptic::myintegrate(inner.f.y, lower = 0.01, upper = 8)
length(y) 21
length(t) 21
length(y) 21
length(t) 21
[1] -1.150277-1.380324i
Upvotes: 3