[程式] R code 多重積分的問題

看板Statistics作者 (黑麻糬)時間13年前 (2012/11/21 22:47), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
[軟體程式類別]: R code [程式問題]: 多重積分 [軟體熟悉度]: 高(1年以上) [問題敘述]: 目前要做一個多重數值積分的結果 內層兩個函數 1. f1(v1,y1) 要把 v1 積掉 2. f2(v2,w,y1) 要把 w 積掉 3. 函數 1 & 2 積完後相乘,接著把 y1 積掉 從頭到尾 w 都是 input 定數 積分結果出現了錯誤訊息 "evaluation of function gave a result of wrong length" [程式範例]: n1=n2=5;CPU1=CPU2=1 A1=2/sqrt(2*pi/(9*n1))*gamma((n1-1)/2)*(2/(n1-1))^((n1-1)/2) A2=2/sqrt(2*pi/(9*n2))*gamma((n2-1)/2)*(2/(n2-1))^((n2-1)/2) A=A1*A2 F1=function(y1) { f1=function(v1) { exp(-9*n1/2*(v1*y1-CPU1)^2)*(v1^(n1-1))*exp(-(n1-1)/2*v1^2) } return(f1) } int1=function(y1) {integrate(F1(y1),lower=0,upper=Inf)$value} F2=function(w,y1) { f2=function(v2) { exp(-9*n2*(v2*(w+y1)-CPU2)^2/2)*v2^(n2-1)*exp(-(n2-1)/2*v2^2) } return(f2) } int2=function(w,y1) {integrate(F2(w,y1),lower=0,upper=Inf)$value} F3=function(w) { f3=function(y1) { A*int1(y1)*int2(w,y1) } return(f3) } int3=function(w) {integrate(F3(w),lower=0,upper=Inf)} 先寫 Global,都是一樣的寫法,int1, int2 函數都沒問題。 但 int3 結果就會出毛病,似乎是資料型態的問題,嘗試了一整天 sapply, mapply, Vectorize 等函數,還是無法克服 希望版上能有大大提供點想法 儘管只是個簡單的三變數 x,y,z 積分也可以,感恩~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.108.134
文章代碼(AID): #1GhEbnPy (Statistics)