Quick guide to Mathematica

To get help, place the cursor on the function and press F1
To run all commands in one cell, place the cursor there and press Shift + Enter or Enter on the numerical keyboard.
Output of many commands is suppressed by ; at the end of the command. If you want to see the output, delete ;
Argument of functions must be in square brackets [...], braces {...} are for arrays, ranges etc., use double brackets [[...]] to access elements of arrays
expr /. {x -> a} means substitute a for x in expr, expr // function means apply function to expr, i.e. it is equivalent to function[expr].
D[f[x],x] = derivative of f[x] with respect to x.
To define a function of x one can use f[x_] := 1+(x^2), notice the underscore and the colon, or # and & like in f := 1+((#)^2) &
N[expr] evaluates expr with machine precision, if you use the decimal point in the expression it will be also evaluated with machine precision
Special characters can be inserted by pressing Esc ... Esc, e.g. Esc p Esc gives π
Clear[...] is used to unset any variables which could have been assigned previously.
Some other useful commands: Simplify, Expand, Factor; Integrate, Series, Sum

Clear all symbols from previous evaluations to avoid problems

Clear["Global`*"]

Basic numerical calculations in Mathematica:

Default setting of Mathematica numerical calculations in terms of exponents

{$MachinePrecision,$MaxExtraPrecision}

This means that maximum relative error of any floating point number is

N[10^-$MachinePrecision]

but actually some of numerical calculations are done using higher precision up to

N[10^(-$MachinePrecision-$MaxExtraPrecision)]

Let us try subtraction of two close numbers:

Print["Pi = ",N[π]//InputForm,", 355/113 = ",N[355/113]//InputForm]

exact=π-355/113;
Print["π - 355/113 = "];
(* Here Mathematica works in higher precision to get the answer correctly *)
Print[N[exact,16] ,"  if we require 16 digits"];
Print[N[exact,20] ,"  if we require 20 digits"];
(* Here Mathematica really works in double precision *)
value1=N[exact]; Print[value1, " if number of digits is not given - double percision evaluation"]
(* To see all digits we have to use a trick *)
Print[N[exact]//InputForm, " if all digits are printed using InputForm"] 
(* But here Mathematica works again in higher precision *)
value2=N[exact,$MachinePrecision]; Print[value2 ,"  if we require $MachinePrecision = ",$MachinePrecision," digits"];
Print["Absolute error of double precision evaluation = ",value1-value2]
Print["Relative error of double precision evaluation = ",Abs[value1-value2]/Abs[value1]]

Using the decimal point explicitly in expressions forces Mathematica to make calculations in machine precision:

π-355.0/113.0
π-355.0/113.0//InputForm

Problems with the floating-point arithmetic:

Sometimes the result depends on the order in which we add up numbers:

Forward and backward evaluation of the sum

(∑_(k=1)^n)(1/((k^2)+k))=(∑_(k=1)^n)((1/k)-(1/(k+1)))=1-(1/(n+1))

n=999999;
exact=1-1/(n+1);
wrong=0.0; (* from the largest value to the smallest one *)
Do[  wrong=wrong+1.0/(k*(k+1.0)),{k,1,n}  ]
correct=0.0;(* from the smallest value to the largest one *)
Do[  correct=correct+1.0/(k*(k+1.0)),{k,n,1,-1}  ]
Print["Exact result: ",N[exact]];
Print["Forward  evaluation: ",wrong//InputForm]
Print["Backward evaluation: ",correct//InputForm]
Print["Relative error of forward evaluation = ",Abs[wrong-exact]/Abs[exact]]

Another example of subtraction of two close numbers:

Sometimes we can use a trick to avoid round-off errors, e.g.

(Sqrt[x + 1]) - (Sqrt[x])=(1/((Sqrt[x + 1]) + (Sqrt[x])))

x=10^8;
flx=1.0 x;
exact=N[Sqrt[x+1]-Sqrt[x],16];
wrong=Sqrt[flx+1]-Sqrt[flx];
correct=1/(Sqrt[flx+1]+Sqrt[flx]) ;
Print["Exact evaluation: ",exact,16]
Print["Incorrect evaluation: ",wrong//InputForm]
Print["Relative error = ",Abs[wrong-exact]/Abs[exact]]
Print["Correct evaluation: ",correct//InputForm]
Print["Relative error = ",Abs[correct-exact]/Abs[exact]]

Solving the quadratic equation can be tricky:

Clear[x];
k=5; s=-1;
N[Solve[x^2+s*10^k x+1==0,x],50]
N[Solve[x^2+s*10^k x+1==0,x]]

Straightforward solution:

a=1.0;
b = s*10.0^k;
c=1.0;
d=Sqrt[b*b-4*a*c];
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
x2alt = c/(a*x1);
{x1,x2,x2alt}//InputForm

Evaluation of the exponential function using Taylor:

n=100;
x = 10;
ex=1.0;
Do[  ex=ex+x^k/(k!),{k,1,n}  ]
emx=1.0;
Do[  emx=emx+(-x)^k/(k!),{k,1,n}  ]
Print["Exact ",N[{Exp[x],Exp[-x]},16]]
Print["Double ",{ex, emx,1/ex}//InputForm]
Print["Relative error = ",Abs[emx-1/ex]/Abs[1/ex]]

A textbook example:

nn=15;aa=10;
Int[n_,a_]:=Integrate[z^n/(z+a),{z,0,1}];
Int[nn,aa]
exactInt=N[Int[nn,aa],16]
wrongInt=1.0(-aa)^nn Log[(aa+1)/aa];
Do[ wrongInt=wrongInt+(-aa)^k Binomial[nn,k]((1+aa)^(nn-k)-aa^(nn-k))/(nn-k),{k,0,nn-1}  ]
Print[{exactInt,wrongInt}//InputForm]
Print["Relative error = ",Abs[exactInt-wrongInt]/Abs[exactInt]]

Forward recurrent relation:

stillWrongInt=1.0Log[(aa+1)/aa];
Do[ stillWrongInt=1/k-aa stillWrongInt,{k,1,nn}  ]
Print[{exactInt,stillWrongInt}//InputForm]
Print["Relative error = ",Abs[exactInt-stillWrongInt]/Abs[exactInt]]

Backward recurrent relation:

correctInt=1.0;
Do[ correctInt=1/((k+1) aa)-correctInt/aa,{k,nn+16,nn,-1}  ]
Print[{exactInt,correctInt}//InputForm]
Print["Relative error = ",Abs[exactInt-correctInt]/Abs[exactInt]]