r - R 中的 2 倍(重复)积分

标签 r integral numerical-integration

例如,我想在 R 中计算 2 重重复积分(不是二重积分),

哪里

实际上,f(x) 和 g(x) 都相当复杂,但是为了进行实验,让我们简化 g(x)=1 和 f(x)=cos(x),在 R 中我使用 积分来计算:

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

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

我收到此错误消息:

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

知道如何做到这一点吗?

最佳答案

所需要的只是

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

比较

phi(1)
#[1] 0.4904915

phi(1:3)
# [1] 0.4904915

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

integrate 需要一个向量化函数。

关于r - R 中的 2 倍(重复)积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340846/

相关文章:

R:堆叠面积图不堆叠

c - C 中的反导数

r - 用 R 进行三重积分计算

python - 仅在正面积的 numpy 数组中集成

python - 如何将时间间隔分成不同长度的部分?

r - 指定 ggplot2 标签选项而不传递标签值

r - 为什么当少于 50% 的经历事件时生存曲线总和为 100%?

r - R中Twitter情感分析中的图释

python - 使用 SciPy 积分本身涉及积分的函数

matlab - 找不到显式积分