Comparison of Integration
Steven Miller: Williams College: sjm1@williams.edu

Print[Hyperlink["https://mathworld.wolfram.com/SimpsonsRule.html"]];

integratecompare[a_, b_, num_, ff_]:= Module[{},
(* need num even for simpson's rule *)
f[x_] := ff[x];
currf = f[a];
left = 0;
average = 0;
right = 0;
simpson = 0;
deltax = (b-a)/num;
simpson = simpson + currf * deltax  * 1/3;
(* use current and new to minimize computations *)
(* don't compute same value for each method, do once and add! *)
For[n=1, n ≤num, n++,
{
newf = f[a + n deltax];
left = left + currf * deltax;
average = average + deltax*(currf + newf)/2;
right = right + newf * deltax;
simpson = simpson + If[Mod[n,2] == 0, 2, 4] * newf * deltax / 3;
currf = newf;
}]; (* end of n loop *);
simpson = simpson - currf* deltax / 3; (* need to fix last term *)
answer =  SetAccuracy[Integrate[f[x], {x,a,b}],15]
Print["Function is ", ff[x]];
Print["Going from ", a, " to ", b, " with ", num, " divisions."];
Print["Left     = ",  SetAccuracy[left,15]];
Print["Average  = ", SetAccuracy[average,15]];
Print["Right    = " , SetAccuracy[right,15]];
Print["Simpson  = " , SetAccuracy[simpson,15]];
Print["Answer is  ", SetAccuracy[answer,15]];
Print[" "];
Print["Below are the errors."];
Print["Left:     = ",  SetAccuracy[left-answer,15]];
Print["Average:  = ", SetAccuracy[average-answer,15]];
Print["Right:    = " , SetAccuracy[right-answer,15]];
Print["Simpson:  = " , SetAccuracy[simpson-answer,15]];
Print["(Delta x)^1: ", SetAccuracy[deltax, 15]];
Print["(Delta x)^2: ", SetAccuracy[deltax^2, 15]];
Print["(Delta x)^3: ", SetAccuracy[deltax^3, 15]];
Print["(Delta x)^4: ", SetAccuracy[deltax^4, 15]];
Print["(Delta x)^5: ", SetAccuracy[deltax^5, 15]];
]

inputf[x_] := Cos[(Pi x)^2];
integratecompare[0,1,1000,inputf]

Plot[Cos[(Pi x)^2], {x,0,1}]