Programming with Mathematica

D. E. Soper, S. J. Remington
30 Sept. 2004

Built in functions

Mathematica has functions that calculate something,

Sin[0.1]

Mathematica has functions that do something,

Plot[Sin[x],{x,0,1}]

The functions that do something generally also return a "value"

plot1 = Plot[Sin[x],{x,0,1}]

plot2 = Plot[Cos[x],{x,0,1}]

Show[plot1,plot2]

To find out about Mathematica functions (or anything else in Mathematica), try the Help browser. Note that Mathematica cares about case. It will not recognize, for example:

sin[0.1]

Lists

Mathematica likes to work with lists, where a list is a bunch of things enclosed in braces and separated by commas. For example a list of numbers could be {1,7,4,4,48}. Here, we make a list, called "results" of the form {{0,0},{1,0},{2,0),(3,0},...} using the built-in function "Table." Each list element is itself a list of the form {n,0}. In the Help Browser, type Table to find out more about the Table function. You can append items to lists with Append, etc.

xlist={1}

xlist=Append[xlist, 2]

ntot=5; (* initialize a list with 5 numbered elements *)
results = Table[{n-1,0},{n,1,ntot}]

In listspeak, the first element of the 5th element of this list is

results[[5,1]]

while the second element of the 5th element of this list is

results[[5,2]]

To make a [[  you can type "escape", "[","[", "escape",while to make a ]] you can type "escape", "]","]", "escape". You can also use

results[[5,1]]

but it's not as pretty.

Write your own functions

You can write your own function. The variable name, here x, is denoted x_ indicating that x can take any value. You collect variables internal to the function, in this case y, as the first part of the Module.

xplusone[x_] := Module[{y},y = x + 1]

Try it.

xplusone[4]

xplusone[t]

A function can have several steps, separated by semicolons. The "answer" is the result of the last step.

ff[r_] := 
Module[{x,y,z},
x = r + 1;
(* This is a comment *)
y = 1/x + 5;
(* You can put a comment anywhere *)
z = Sin[y]
]

q = ff[t]

ff[0.1]

A function can do something. The "answer" is the result of the last step.

fff[r_] := 
Module[{x,y,z},
Print["Starting with r = ",r];
x = r + 1;
Print["Add 1: ",x];
y = 1/x + 5;
Print["Invert and add 5: ",y];
z = Sin[y];
Print["Take the sine of that: ",z];
z
]

fff[t]

Loops

The instruction Do[ something, {n,1,8}] does what ever "something" is with n = 1, then does it again with n = 2, then does it again with n = 3, et cetera. It stops after it does "something" with n = nmax. (This Module has an empty list of internal variables.)

sorry[sin_,nmax_] :=
Module[{},
  Do[
    Print[n, ". I will not ",sin," any more."],
    {n,1,nmax}
    ]
]

sorry["write on the wall",3]

Logical functions

Some functions give values True or False.

4 < 5

4 > 5

Branches

You can use logical functions and the If[ ...] construct to get the computer to do different things depending on the outcome of some test. This instruction will set z to 3 if n is less than 4 and will set z to 0 otherwise.

n = 2

If[n<4,z=3,(*else*)z=0]

If you leave out the "else" part, it does nothing when the condition is not satisfied.

n = 3

If[n<4,Print["n<4"]]

We can use this in a function designed to do one thing if x < 0.3 and another otherwise.

h[x_] :=
Module[{p},
  p = 0.3;
  If[x<p,
    Print[x," is less than ",p,"."],
    (*else*)
    Print[x," is greater than or equal to ",p,"."]
    ]
    ]

h[0.2]