集成错误:函数评估给出了错误长度的结果

问题描述 投票:0回答:2

试试下面的代码:

library(pracma)

f <- function(x) 1

integrate(f,0,1)$value
quad(f,0,1)

quad() 工作正常但 integrate() 报告错误信息:

集成错误(f,0,1): 函数的评估给出了错误长度的结果

这个 integrate() 应用程序有什么问题?提前致谢!

r integration
2个回答
8
投票

你可以试试:

integrate(Vectorize(f),0,1)$value

参见

integrate
的手册:
f
应该是一个R函数,它接受一个数字第一个参数并返回一个相同长度的数字向量。
Vectorize
将使
f
返回与输入长度相同的输出的函数。


0
投票

解决方案的另一个说明:

integrate(function(x) rep(1, length(x)), 0, 1)
© www.soinside.com 2019 - 2024. All rights reserved.