next up previous
Next: Adding to the Simulation Up: Approximating the Solution of Previous: Defining Commands for a

Combining the Commands into a Mathematica Module

We can do a lot better job by defining a function which performs the simulation for us. Mathematica allows you to write your own functions or Modules. This is not a hard thing to do. All you need to do is combine the input lines that perform the simulation into a function. To show how this is done, the following is an example module which will simulate a single gambler playing the game until the goal is achieved or the money is gone.

  GamblersRuin[ a_, c_, p_ ] :=
              Module[ {ranval,var1,var2,var3},
                    var1 = a;
                    var2 = c;
                    var3 = p;
                    While[ 0 < var1 < var2,
                         ranval = Random[];
                         If[ ranval < var3, var1 = var1 + 1, var1 = var1 - 1]
                         ];
                    Return[var1==var2]
                    ]
There are several things to notice in the example. First, this is the same thing we have done in the past to define a function. That is, we have a function name GamblersRuin with three input variables, a, c, and p. The operator ":=" is used to start the definition. Secondly the function involves the Mathematica command Module. This just tells Mathematica to perform all the commands in the module (like a subroutine in Fortran or method in C++). There are some special features we need to understand in the Module command. The Module command has two arguments. The first argument is a list of all the local variables that will only be used inside the module. In the example above the local variable list is
   {ranval,var1,var2,var3}
These variables are only used in the module and are cleared once the module is done executing. The second argument is all the commands that will be executed each time the module is called. There are some assignment commands at the beginning that are used to make things cleaner. The module uses temporary variables so that the values of the input variables are not overwritten when the module executes. Notice that the commands in the module are exactly the commands we used in the previous section with different variable names.

The last command is added to our previous list of input lines to return a result from the work done by the Module. Without this we would never get any results from out calculation. Any recognized variable type or structure within Mathematica can be returned by a Module. In the example Module, the returned value is the result of testing two variables in the code for equality. The code fragment

  var1==var2
tests to determine if the variables, var1 and var2, are equal. If the two are equal the line outputs True and if the two are not equal, the line outputs False. A closer look at the code tells that the variable var1 keeps track of the amount of money the gambler has and the variable var2 is the amount of money the gambler wants to leave with. So a returned value of True means the gambler achieved the goal and a returned value of False indicates the gambler leaves with no money.

Again, all but the last command must be ended by a semicolon. This is to make sure that the commands are separated in the execution. Commands separated by blank space will be considered as terms to be multiplied together. Leaving out the semicolon will give rise to lots of error messages, wrong results or both.


next up previous
Next: Adding to the Simulation Up: Approximating the Solution of Previous: Defining Commands for a
Joe Koebbe 2003-10-01