2017-05-06 93 views
1

的結果,嘗試下面的代碼:錯誤的整合:功能評價給了錯誤的長度

library(pracma) 

f <- function(x) 1 

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

四()正常工作,但集成()報告錯誤消息:

錯誤集成(F,0,1)的功能 評價給出了錯誤長度的結果

什麼是錯的這個集成()應用程序?提前致謝!

回答

2

可以嘗試:

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

看到的integrate手冊:F應的R函數採取一個數字的第一個參數和返回相同長度的數值向量。 Vectorize將使f成爲一個返回與輸入相同長度輸出的函數。

+0

它的工作原理,但爲什麼呢? @ mt1022 – Lin

+0

看到我的編輯解釋。 – mt1022

0

解決方案的另一個例證:

integrate(function(x) rep(1, length(x)), 0, 1)