Simulation Programming in Mathematica

Carl T. Bergstrom

Objective

The purpose of this tutorial is to provide a basic introduction to the use of Mathematica as a programming language for writing scientific simulations, with a particular focus on simulating stochastic processes.

The tutorial assumes a basic familiarity with the use of Mathematica, including the basic syntax, graphics commands, and use of functions.  Some experience with a conventional "procedural" programming language, such as BASIC, Pascal, or C, is also assumed. Examples in Pascal or procedural "pseudocode" will be provided, to highlight the differences in programming philosophy and methodolgy between these languages and Mathematica. If you don't have previous programming experience, not to worry. You are probably better off,  for there will be less to be unlearned. In this case, you can more or less skip over the examples of procedural programming, and simply concentrate on the Mathematica solutions.

Introduction: Mathematica as a programming language

When was first introduced to Mathematica, I employed it primarily as a tool for doing symbolic calculations, a calculator for algebraic expressions, if you will. When I needed to run simulations, I would turn to a procedural programming language such as Pascal. Eventually, I became tired of switching back and forth, and disovered that Mathematica itself provides an excellent environment for simulation programming. In this tutorial, I hope to share my own experiences using Mathematica in this way.

Should you bother to learn Mathematica, especially if you are already comfortable programming in C, Pascal, Basic, or a similar langauge?  Of course everyone will have to decide this for himself or herself, but I have found that Mathematica offers a number of advantages over these languages for simulation programming. Let me spin a moment's worth of propaganda, and then in the subsequent sections of the tutorial, you can see for yourself whether any of these reasons are worthwhile.

The pro and cons of using Mathematica for simulations


A final note:
In one sense, learning to program in Mathematica is easy. Anyone who has some experience using the program for symbolic computation will already know much of the syntax and therefore will spend relatively little time flipping through the appendices of programming manuals. In another sense, however, learning to program in Mathematica can be quite challenging, especially for anyone accustomed to procedural programming languages. In order to program in Mathematica as it was intended, one needs to throw out the habits and ways of thinking that come with procedural programming, and embrace a new programming style. Rather than a general discussion of the subject (the interested reader will find a thorough treatment in Roman Maeder's (1996) Programming in Mathematica) let us move directly to an example.

Example 1: Simulating rolls of a die

Example 2: Two simple recursive processes

Example 3: Pólya's Urn


Converted by Mathematica      November 5, 1999