I think this might be what you want: ex1.fcn<-function(x){ h<-4/(1+x^2) return(h) }
my.pi <- function(sample.size, sim.size, low, hi, func) { # create output vector result <- numeric(sample.size) for (i in seq(sample.size)){ # loop for 'sample.size' times my.rand.x <- runif(sim.size, low, hi) # create random numbers result[i] <- ((hi - low) / sim.size) * sum(func(my.rand.x)) } return(result) # return the vector } pi.MC <- my.pi(100, 1000, 0, 1, ex1.fcn) # call the function hist(pi.MC) # plot histogram On 9/29/07, Letticia Ramlal <[EMAIL PROTECTED]> wrote: > Hello: > I am a bit confused by this problem. Can anyone give me some advice on this I > would greatly appreciate it. Thank you for all your help. > > Need to create a for loop that saves the estimate of pi from each 0f 100 > separate iterations and store it in a numeric vector (of length 100). The for > loop should be placed in a function that allows the user to vary the sample > size, the simulation size, the integration limits and the input function. In > addition construct a histogram of the estimates and include a red vertical > line at pi. > > ex1.fcn<-function(x){ > h<-4/(1+x^2) > return(h) > } > n=1000 > a=0 > b=1 > my.rand.x=runif(n,min=a,max=b) > pi.MC = ((b-a)/n)*sum(ex1.fcn(my.rand.x)) > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.