r - How to perform sum of vector elements if it contains another vector? -
i have similar code in r gives warnings:
> require(pracma) # integral > v1 <- c(1, 2, 3, 4, 5) #some vector of unknown length > v2 <- c(6, 7, 8, 9, 0) #another vector of same length v1 # here want sum v1 & v2 elements, sum contains x argument > f <- function(x) x^(-2i) sum(v1 / (1 - v2 * x)^2) > integral(f, 1e-12, 1) 49: in v2 * x : longer object length not multiple of shorter object length 50: in v1/(1 - v2 * x)^2 : longer object length not multiple of shorter object length
i use r-studio debugging purpose, , see integral function pass vector f function.
i understand v2 vector , x vector. i'm don't know how make work without warnings. have tried put vectorize(x) in sum function, warnings still here.
how handle situation?
there 2 problems here. first need vectorize function. second function returns complex value (although argument real). can integrate complex functions using myintegrate(...)
function in elliptic
package.
library(elliptic) v1 <- c(1, 2, 3, 4, 5) v2 <- c(6, 7, 8, 9, 0) f <- function(x) x^(-2i) * sum(v1 / (1 - v2 * x)^2) g <- vectorize(f) myintegrate(g, 1e-12, 1) # [1] -3173.272-8530.861i
Comments
Post a Comment