Chris's guide to Mathematica


Hello Everybody
Mathematica, like java or c++ is as complicated as you want to make it, much of the functionality that mathematica has will rarely be used, such as finite field analysis or fourier analysis on p-adic metrics (smile and nod), I'll try and shed some light on the structure of mathematica programs, input and output and some equations solving.

Structure
Mathematica, does not have the programming structure of other languages, in fact i would even call it a language myself. First of all assignments are fairly easy to start but gets rather difficult with assignment of functions.

x = pi assigns pi (3.14159...) to the variable named x
f[x] = x + 1 this will assign x+1 to f[x], where x is a variable
f[x] := x + 1 this will delay the computation to when f is used
f[x_] := x + 1 x is now an expression, you may neglect the colon,
but i strongly recommend it

(The main difference between x_ and x, is that x_ will be evaluated when you call f, so we can say f[f[x]] and in this case it will return x+2 instead of f[x+1])

If you wish to make a set, the assigment is again quite natural

A = {x,y,z} this is like a one dimensional array
B = {{x1,x2},{y1,y2},{z1,z2}} is like a 2-d array (3x2 Matrix)

If you wish to get the elements out of a set use part Part[B,2,1] returns y1, it is the second set, the first element MatrixForm[(Set)] will display it in a nicer form. And Length[(set)] give the number of elements in the set.

If you wish to loop, then there are 2 main ways a do loop, (Note: if a break statement is encounted, it will exit a loop)

Do[(expression);(exp);(exp)....;(exp),{i,1,100,stepsize}] will execute the expressions making i go from 1 to 100, leaving the stepsize will assume a stepsize of 1

While[(condition),(exp);...;(Exp)] will execute statements if the condition is satisfied.

If[(condition),(exp);..;(exp)], if condtion is true do this for little old me. ( This can be used in functions, but one must use the :=, not the = )

A bit about conditions, we have the following

x == y DOUBLE EQUALS! everybody forgets this (even me sometimes). This is a test of whether x is the same as y, and not an assignment of y to x.
x < y x is strictly less than y
x > y x is strictly greater than y
x >= y x is more than or equal to y
x <= y x is less than or equal to y

The last is equation Solving and some ploting

Solve[(expression list),(varaiable list)]
This will solve algebraically the set of expressions, if it is a set remember the {} otherwise just leave the {} out, same goes for the variable list.

NSolve[(expression list),(variable list)]
FindRoot[(expression list),(variable list,initial cond),...,(v,i)] NSolve can be used just like solve but FindRoot requires an initial secant or initial condition.
eg FindRoot[{x+y==2x+1,Sin[x]==Cos[y]},{x,1},{y,2}]

Differential Equation Solving
Algebraic
DSolve[{(exp list)},function,variable]
eg sol = DSolve[{y''[x]== -y[x],y'[0]==1,y[0]==0},y[x],x]
Will return Sin[x], mathematical note, if the number of initial conditions is not equal to the order of the equation, it will use constants, in mathematica they are C[1],C[2],....
generally if mathematica cant solve it, it doesn't have a solution

Numerical
getting inteligible answers from numerical solutions isn't obvious sol = NDSolve[(explist),function, {variable+range}] will return an interpolating function, essentially a solution
sol = NDSolve[{y''[x]== -x,y'[0]==1,y[0]==0},y[x],{x,0,2 pi}]
will return {{y[x]\[Rule]InterpolatingFunction[{{0.,6.28319}},"<>"][x]}} which is basic double dutch to me. (I only know dutch swearwords)

To plot this solution, given sol, all we neet to do is reference y[x] wihtin sol, this is done with /.
Plot[y[x]/.sol,{x,0,2 Pi}]
And to obtain a specific value at a given value, se set our variable say x = 1, and evaluate with Evaluate[y[x]/.sol] (this can be within a loop if needed)
if your function is actually parametric, say you have a function of t to (x[t],y[t])
Plot[{x[t],y[t]}/.sol,{x,0,2 Pi}] will plot the solution.

So here's about all the functionality you need, here is a nice little example program that will do the same functionailty as our little fortran program.

A = ReadList["bob.dat",Real]
out = OpenWrite["fred.dat"]
Do[ bob = Part[A,i];
fred = bob*bob;
fred >>> out
,{i,1,Length[A]}]
Close[out]

Simple as that (even shorter than fortran).

So thats chris's guide to mathematica
If you have any questions mail me
Also, take a look at Chris's Guide to Fortran

Take me Home contry Roads