time<-c(65,130,195,260) #in days
simulation<-matrix(numeric(10*4),10,4)
#vessel matrix containing the desired number of simulations for 1 initial case
results<-NULL
#initial<-.5
initial=seq(from=0.5, to=10, by=.5) # Changed This to be vector
#This would be one case and I've got a vector with approx.2000
#loop
## Writing to a function to help in reusing the code
MM <- function(initial){
for(i in 1:10){
simulation[i,] = (initial*exp(-10*(time/260)) +
20*(1-exp(-10*(time/260))) +
20*(1000/260))*rnorm(1)
}
results<-colMeans(simulation)
return(results)
}
sapply(initial, FUN=MM) # This prints the colmeans for each inputted element in the vector initial
#Output:
# [,1] [,2] [,3] [,4]
#[1,] 0.04560954756720 -7.358191844904 -8.098682864513 3.665639204753
#[2,] 0.04631255769454 -7.468652842648 -8.217009769192 3.717726620815
#[3,] 0.04637026427987 -7.477720033496 -8.226722633000 3.722002216291
#[4,] 0.04637500112485 -7.478464313845 -8.227519913413 3.722353178540
# [,5] [,6] [,7] [,8]
#[1,] 18.98057953410 21.03382334801 11.96959043580 63.54595548535
#[2,] 19.24268217595 21.31585924907 12.12529951449 64.34721302134
#[3,] 19.26419687094 21.33901016563 12.13808089400 64.41298424507
#[4,] 19.26596290465 21.34091050858 12.13913005352 64.41838307588
# [,9] [,10] [,11] [,12]
#[1,] -20.01917307564 12.56144497430 27.31607000721 -17.70877647896
#[2,] -20.26360373127 12.70980687528 27.62780928081 -17.90382189201
#[3,] -20.28366782130 12.72198516172 27.65339839866 -17.91983219447
#[4,] -20.28531478210 12.72298481635 27.65549888136 -17.92114640013
# [,13] [,14] [,15] [,16]
#[1,] -1.163127428493 -13.07282052205 20.91363938663 -15.43763356270
#[2,] -1.175475358083 -13.20640615074 21.11903955938 -15.58312496123
#[3,] -1.176488937866 -13.21737152689 21.13589983227 -15.59506762248
#[4,] -1.176572137562 -13.21827161978 21.13728380775 -15.59604793581
# [,17] [,18] [,19] [,20]
#[1,] 4.129982905656 -32.29365824471 -26.80599719664 -13.92279757936
#[2,] 4.167268037791 -32.57240758567 -27.02676696018 -14.03195652202
#[3,] 4.170328587811 -32.59528872494 -27.04488884592 -14.04091683368
#[4,] 4.170579813055 -32.59716692322 -27.04637638088 -14.04165234085
당신은 기능의 가족을 적용 사용해야합니다. 희망이 도움이!
'a','b','c' 및'j'는 무엇입니까? 그들은 상수입니까? 'for' 루프를 실행할 때마다 똑같은 것을 얻지 않겠습니까? – nograpes
@nograpes 거기에'rnorm (1)'이 숨어있다. – josliber