Saturday, March 24, 2012

RecurrenceTable - Introduction

RecurrenceTable defines a sequence by setting f[n+1] equal to some function of f[n], and an initial condition for f[1]. Recurrence relations are also called difference equations and the method of finite differences uses recurrence relations. These are discrete versions of differential calculus. See the excellent articles in Wolfram Mathworld.

In our computer simulations of neural circuits, while the equations governing the neurons' behavior are phrased as differential equations, in reality we compute them as difference equations. For one thing we add noise to them, and for another, in practice they contain singularities. The "forward Euler method" et cetera won't work. I suspect that many prima facie differential equations are, in practice, unsolvable, and mask an underlying reality actually described by difference equations.

Here is a comparison between Table and RecurrenceTable that simply shows how to use the syntax of RecurrenceTable.


In[21]:= powersOfOneHalf = Table[2^-i, {i, 1, 25}] // N

Out[21]= {0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, \
0.00195313, 0.000976563, 0.000488281, 0.000244141, 0.00012207, 0.0000610352, \
0.0000305176, 0.0000152588, 7.62939*10^-6, 3.8147*10^-6, 1.90735*10^-6,
 9.53674*10^-7, 4.76837*10^-7, 2.38419*10^-7, 1.19209*10^-7, 5.96046*10^-8,

In[23]:= RecurrenceTable[{a[n + 1] == .5 a@n, a@1 == 0.5}, a, {n, 25}]

Out[23]= {0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, \
0.00195313, 0.000976563, 0.000488281, 0.000244141, 0.00012207, 0.0000610352, \
0.0000305176, 0.0000152588, 7.62939*10^-6, 3.8147*10^-6, 1.90735*10^-6,
 9.53674*10^-7, 4.76837*10^-7, 2.38419*10^-7, 1.19209*10^-7, 5.96046*10^-8,

We can define f[n] in terms of more than one preceding term. If so, we need to supply the initial conditions for as many terms as specify the recurrence relations. Here is a Fibonacci-type series, a different flavor defined using three preceding terms rather than two. The initial conditions can also be thought of as boundary conditions.

In[4]:= RecurrenceTable[{a[n] == a[n - 1] + a[n - 2] + a[n - 3], a[1] == 1, a[2] == 1,
   a@3 == 1}, a,
   {n, 10}]

Out[4]= {1, 1, 1, 3, 5, 9, 17, 31, 57, 105}

Saturday, March 10, 2012

Beginners: Mathematica Front End, Kernel, and Notebooks

Front End and Kernel

There are two components to Mathematica: the Front End and the Kernel. Unless you decide to use a command-line (aka "text-based") interface at a terminal, you are using the graphical user interface (GUI) of Mathematica called the Front End. If you program in Mathematica, the Front End is also your programming environment. You will find the Front End to be a superior GUI and programming environment once you get used to it. However the part of Mathematica that does the computation you request through the Front End is called the Kernel. The Kernel can run on the same computer as the Front End or on remote computers.

Mathematica Notebooks 

Most often you work in a Mathematica Notebook (the filename ends in .nb). Think of a Notebook as an electronic laboratory notebook attached to a very powerful computer (the kernel). You can do almost anything you can think of in a Notebook, such as write text, mathematical formulae, a program, or even a paper, book or webpage. You can communicate with Mathematica's help files, which are in the Documentation Center, request information, perhaps on a function, to be displayed right in the notebook where you are working, or in a separate browser within the Mathematica environment. And you can communicate with the kernel and ask it to do a simple or enormously complicated computation, even if the kernel is located at a remote site.

The Mathematica Notebook consists of a hierarchy of nested cells. You select an entire cell by clicking it's bracket in the right margin. You select contents within a cell in the normal way. Within a cell, the insertion point is a prominent blinking vertical bar, while the cursor is a vertical bar with a capital and base (an "I-beam"). But note that between cells, the insertion point is a prominent horizontal bar running all the way across the notebook, while the cursor is a horizontal I-beam that disappears when not quite between the cells.

You will soon automatically notice whether a cell bracket\[LongDash]the vertical, terminated bar at the right of the cell\[LongDash]is denoted at the top by two small horizontal lines or a triangle. The two small lines indicate an open cell, and the triangle indicates a closed cell. To open the closed cell and view its content, double-click anywhere on the cell bracket. Likewise, in the left margin, open cells are indicated by an upward arrowhead (an angle bracket) and closed cells by a downward arrowhead, and you can toggle open or closed by clicking on the arrowhead.

Clicking Enter on the alphabetic keyboard is equivalent to Return, not Enter. Shift-Enter on the alphabetic keyboard and Enter on the numeric keypad (provided NumLock is on) are equivalent and tell Mathematica to evaluate the current cell if it is an input cell. What "evaluate" means is explicitly explained in Mathematica if you need to understand the details.

The first time you click Enter (in an input cell) after starting a Mathematica session, there is a delay while the Kernel is started and performs its evaluation of your request.

A Mathematica mantra is "Everything is an Expression," which means that the universal, unifying syntax of Mathematica is a normal form expression with a head and argument: head[argument]. Suffice it to say here that the Mathematica Notebooks are entirely rendered via expressions and every aspect of Notebooks are potentially under your control.

<Shift> + <Enter> causes Mathematica to evaluate the contents of the cell containing the input point of that you have selected by clicking its bracket. Remember that the selected cell may not be the last cell in your Notebook, but rather, if you have "skipped around" it is the last cell you have evaluated. Also critical is that the selected cell is processed in light of all previous evaluations, no matter in what order they appear in the notebook. You can always tell what the kernel has seen by the Input and Output line numbers. I.e., that last expression the kernel processed was sent to it in the cell with the highest line number.

You will find it convenient to not always move sequentially down a Notebook but rather to stay in one cell or group of cells, or to skip around a bit. But this will not work unless you pay attention to what the kernel has seen, often using line numbers. Sometimes it is easier to just clear your definitions are start from scratch than to figure out what the kernel has seen.

These mechanics will soon become second nature and you will fall in love with the Mathematica Notebook, your working document in the elegant, unique Mathematica graphical user interface (the "front end").

RecurrenceTable and NestList

RecurrenceTable and NestList do the same thing—build a list of values by repeatedly applying a function to an initial expression. However NestList is the more general function and RecurrenceTable is specialized to handle recurrence relations, and the syntax of each reflects this. Here is a simple comparison. See the examples in the Doc Center for RecurrenceTable for more sophisticated examples.

In[1]:= NestList[Sqrt, 100.0, 5]

Out[1]= {100., 10., 3.16228, 1.77828, 1.33352, 1.15478}

In[2]:= NestList[#^2 &, Last@%, 5]

Out[2]= {1.15478, 1.33352, 1.77828, 3.16228, 10., 100.}

In[3]:= RecurrenceTable[{a[n + 1] == Sqrt@a@n, a[1] == 100}, a, {n, 1, 5}] // N

Out[3]= {100., 10., 3.16228, 1.77828, 1.33352}

In[4]:= RecurrenceTable[{a[n + 1] == a[n]^2, a[1] == Last@%}, a, {n, 1, 5}]

Out[4]= {1.33352, 1.77828, 3.16228, 10., 100.}