2-fold (repeated) integral in R

左心房为你撑大大i 提交于 2019-12-13 16:39:24

问题


I want to compute a 2-fold repeated integral (not double integral) in R, for example,

where

In practice, both f(x) and g(x) are quite complicated, but to run an experiment, let's simplify g(x)=1 and f(x)=cos(x), in R I use integrate to compute:

> phi = function(x){integrate(function(x){cos(x)},lower=x,upper=3)[["value"]]^2}

> foldintegral = integrate(phi,lower=0,upper=3)

And I got this error message:

Error in integrate(phi, lower = 0, upper = 3) : 
  evaluation of function gave a result of wrong length

Any Idea how to do that ?


回答1:


All that is needed is

integrate(Vectorize(phi), lower=0, upper=3)
# 1.067943 with absolute error < 1.2e-14

Compare

phi(1)
#[1] 0.4904915

phi(1:3)
# [1] 0.4904915

Vectorize(phi)(1:3)
# [1] 0.4904915 0.5900965 0.0000000

integrate expects a vectorized function.



来源:https://stackoverflow.com/questions/40340846/2-fold-repeated-integral-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!