Re: Easy question, please help to run a function n times
- To: mathgroup at smc.vnet.net
- Subject: [mg109615] Re: Easy question, please help to run a function n times
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 8 May 2010 07:07:05 -0400 (EDT)
On 5/7/10 at 6:28 AM, kxcarmichael250 at hotmail.com (Kevin) wrote: >I am trying to run the function t= M/(R/n), where R is a random >number generated between 15 and 25. What function do i use to run >this simulation 10,000 times and calculate the mean and std error >from 306. >M=74 >n=88 >No=306 >R= RandomReal[{15,23}] You do not want to use Set here. with this code, RandomReal[{15,23}] will evaluate and the result will be assigne to R. All future references to R will use this value, not a new random value. To get a new random value every time R is used this last should be r:= RandomReal[{15,23}] Note also, I've used a lower case letter rather than the upper case you chose. All internal Mathematica symbols start with an upper case letter and several consist of a single upper case letter. By using lower case letters for your variables you are guaranteed they will not conflict with pre-existing Mathematica names. >t= M/(R/n) This assigns a fixed value to t >t = x; This assign whatever x evaluates to t and does nothing useful given the rest of your code >Do[t = M/(R/n), {10000}] This assigns the same fixed value of t that was used earlier and does it 10,000 times. >Everything is working for me except how to run the simulation 10,000 >times Your code is valid Mathematica syntax and will run without error. But it certainly does not solve the problem you've indicated you are trying to solve. The simplest way I can think of to solve your problem is to do r=RandomReal[{15,23},10000]; That generates a list of 10,000 random reals between 15 and 23 and assigns it to r. Now the mean and standard deviation can be easily computed as: Mean[74/(r/88)] StandardDeviation[74/(r/88)] or as the difference from 306 Mean[74/(r/88) - 306] StandardDeviation[74/(r/88) - 306] Also note, I've used Set (=) when defining r. If SetDelayed (:=) were used here, the list of random values would used for computing the mean would be different than those used for computing the standard deviation.