(* Mathematica program to print finite difference apprximation table *)
(* approximation of derivative of sin(x) using different step size *)
(* Nasser M. Abbasi, oct 2, 2010 *)

h={0.1,0.05,0.01,0.005,0.001};
exact=Cos[1];
derivativeRight[h_]:=(Sin[1.0 +h]-Sin[1.0 ])/h;
derivativeLeft[h_]:=(Sin[1.0]-Sin[1.0-h])/h;
derivativeCenter[h_]:=(Sin[1.0 +h]-Sin[1.0 -h])/(2.0*h);
derivative3rdOrder[h_]:=(2.0*Sin[1.0 +h]+3.0*Sin[1.0]-6.0*Sin[1.0-h]+Sin[1.0-2.0 *h])/(6.0*h);

data=Table[{h[[i]],
derivativeRight[h[[i]]]-exact,
derivativeLeft[h[[i]]]-exact,
derivativeCenter[h[[i]]]-exact,
derivative3rdOrder[h[[i]]]-exact},{i,1,Length[h]}
];

ScientificForm[TableForm[SetPrecision[data,MachinePrecision],TableHeadings->{None,{"h","D+","D","D0","D3"
}},TableAlignments->Left
],{5,4},NumberFormat->(Row[{#1,"e",#3}]&),NumberPadding->{"","0"}]