Simpson's Rule

Author

Eric W. Weisstein
March 1, 2009

This notebook downloaded from http://mathworld.wolfram.com/notebooks/NumericalMath/SimpsonsRule.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/SimpsonsRule.html.

©2009 Wolfram Research, Inc. except for portions noted otherwise

Code

This code is for the generalized form

SimpsonsRule[f_,{x_,x0_,x2n_},n_Integer:2]:=Module[{h=(x2n-x0)/n},
(Function[x,f][x0]+
4Plus@@Function[x,f]/@(x0+h Range[1,n-1,2])+
2Plus@@Function[x,f]/@(x0+h Range[2,n-2,2])+
Function[x,f][x2n])h/3
]

Example of 3-Point form for sinx on [0, π/2]

<<Utilities`Typesetting`

InterpolatingPolynomial[Table[{x,Sin[x]},{x,0,Pi/2,Pi/2}],x]//Simplify

InterpolatingPolynomial[Table[{x,Sin[x]},{x,0,Pi/2,Pi/4}],x]//Simplify

Sin/@Range[0,Pi/2,Pi/4]

(1/3){1,4,1}.(Sin/@Range[0,Pi/2,Pi/4])(Pi/4)

SimpsonsRule[Sin[x],{x,0,Pi/2}]

Integrate[-((2 x (π-2 (Sqrt[2]) π+4 (-1+(Sqrt[2])) x))/(π^2)),{x,0,Pi/2}]//Factor

N[%]

Plot[{Sin[x],2(x/π),-((2 x (π-2 (Sqrt[2]) π+4 (-1+(Sqrt[2])) x))/(π^2))},{x,0,π/2},PlotStyle->{Black,Red,Blue},
Epilog->{PointSize[.02],Blue,Point[{#,Sin[#]}]&/@(Pi/4Range[0,2])},
AxesLabel->TraditionalForm/@{x,Sin[x]},Ticks->{Table[{x,TraditionalForm[x]},{x,0,Pi/2,Pi/8}],Automatic}]