# source("BMsim2.R") # in R under Windows, use 'change dir' to read from the right place howlong<-system.time( # let's measure now long it takes { n<-1000000 capT<-1 # time horizon; both 't' & 'T' are both reserved tvec<-capT*0:n/n # vectors in R are unit offfset, ie. 1st entry is number 1 (not 0) dW<-c(0,rnorm(n,0,sqrt(capT/n))) # rnorm (n,m,sd) simulates n indep. N(m,sd^2) W<-cumsum(dW) # a bulid-in function that caluculates the cumulative sum # this is much faster than loops freq<-1000 # take out every 1000th point in the simulation & plot it pick<-freq*(0:(n/freq))+1 plot(tvec[pick],W[pick],type='l', xlab="time",ylab="W(time)", main="A Path of Brownian Motion") } ) print(c("It took", round(howlong[3],2), " seconds to do ", n , " points (and to plot)")) # the 3rd coordinate is 'elapsed time', ie. how long it actually takes