1

I need to integrate a function integrand. The function integrand is a product of A and B. A = 2/(upper-lower), and B is the sum of a vector depending on the input parameter.

if I have

 X = 7, 
 N = 50, 
 Ck # a vector of N elements,
 uk # a vector of N elements, 
 upper = 10, 
 lower = -10

and my R-code is as follow:

   integrand<-function(y)
   {
     df<-matrix(,nrow = N,ncol = 1);

     res<-NA;

     for(k in 1:N)
       df[k]<-Ck[k]*cos(y-lower)*uk[k]

     res<-2/(upper-lower)*sum(df);

     return(res)

    }

   integrate(function(x){integrand(x)},upper=X,lower = lower)$value

I got an error message after running the code:

 Error in integrate(function(x) { : 
 evaluation of function gave a result of wrong length

what is my mistake?

Additionally, if df[k]<-Ck[k]*(cos(y-lower)*uk[k]), may I write the code as:

 integrand<-function(y)
  {
    df <-Ck*cos((y - lower)*uk)

    2 * sum(df) / (upper - lower)
  }

 integrate(Vectorize(integrand),upper=X,lower = lower)$value

THANKS!

Smirk
  • 59
  • 8
  • Please do not link to another (closed) question without context. Recognize that eventually, that question will not be visible (assume it will be auto-deleted since it was closed as it was). You should read about [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [minimal examples](https://stackoverflow.com/help/mcve). After that, please reduce your code from the other question (this is hard, I know), generate some representative sample data, and edit this question. – r2evans Jul 02 '17 at 05:59
  • I couldn't write the math formulae here, but I can write them there. Therefore, I just give the link in this post. Do you know the reason why I cannot write the equation here? Thanks! @r2evans – Smirk Jul 02 '17 at 06:03
  • 1
    Because [StackOverflow does not support MathJax](https://meta.stackoverflow.com/questions/348482/how-to-format-mathematical-expressions#comment468954_348482) (and possibly never will). Workarounds: [one](https://meta.stackoverflow.com/questions/348482/how-to-format-mathematical-expressions#comment468954_348482) and [two](https://meta.stackexchange.com/a/76905/300391). My comment stands: this question may suffer from link-rot (the linked question goes away), so you need to make this one completely self-reliant as much as possible. If this means linking images (hackish, I admit), so be it. – r2evans Jul 02 '17 at 06:09
  • However, if this is truly a programming problem (and not a math problem) as suggested by the other question's closure, the formulae may not be necessary. – r2evans Jul 02 '17 at 06:14
  • *What type of data are you using?* (I suggested before that you generate some small, representative, sample data. We have no idea what you are using, and without something *reproducible*, unless the error code is obvious -- which it is not to me, here -- you are unlikely to get much help.) – r2evans Jul 02 '17 at 06:43

1 Answers1

0

Use

integrand <- function(y) {
  mat <- tcrossprod(Ck * uk, cos(y - lower))
  2 * colSums(mat) / (upper - lower)
}

Explanation:

If you read the documentation of function integrate, you see that f must be a vectorized function (i.e. you give it a vector argument and it returns a vector of the same length).

F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • Thanks!! @F.Prive – Smirk Jul 02 '17 at 09:36
  • There is one more question. Please have a look at the edit to my post. Thank you! @F. Privé – Smirk Jul 02 '17 at 11:56
  • Using `Vectorize` seems to work but computations are not really vectorized, so that it should be slower. – F. Privé Jul 02 '17 at 12:03
  • The second `df` is different from the first one. The second `integrand` returns a vector, but I cannot integrate it using `integrate(function(x){integrand(x)},upper=X,lower = lower)$value`. I can do it with `integrate(Vectorize(integrand),upper=X,lower = lower)$value`. The latter code returns a result, but I am not sure if this method is correct. @F. Privé – Smirk Jul 03 '17 at 06:50