(*

## Mathematica Procedures for Validating Riemann Sum Programs

*

* Copyright (c) 1995, 1996 by Jack Courtney

*

*)

`ClearAll[ LHSum, RHSum, Trap, MidPt ];LHSum[ f_, over_List , n_  ]:=  Module[ {a=over[[1]],b=over[[2]],h}        , h=(b-a)/n        ; Return[ Sum[ f[ a+h*i ], {i,0,n-1} ]*h ]        ];RHSum[ f_, over_List, n_  ]:=  Module[ {a=over[[1]],b=over[[2]],h}        , h=(b-a)/n        ; Return[ Sum[ f[ a+h*i ], {i,1,n} ]*h ]        ];Trap[ f_, over_List, n_  ]:=  Module[ {a=over[[1]],b=over[[2]],h}        , h=(b-a)/n        ; Return[ (Sum[ f[ a+h*i ], {i,1,n-1} ]+(f[a]+f[b])/2)*h ]        ];MidPt[ f_, over_List, n_ ]:=  Module[ {a=over[[1]],b=over[[2]],h}        , h=(b-a)/n        ; Return[ h*Sum[ f[ a+h*(i-1/2) ], {i,1,n} ] ]        ];(* Sample: (Uncomment to use) *)(*n=10;a=-6.5;b= 6.5;f[x_]=Sin[ (x+6)*Pi/17 ];Print[ "Check Values for Numerical Integration:" ]Print[ "" ];Print[ "  Integration of ", OutputForm[ f[x] ], " on (",a,", ",b"):" ];Print[ "" ];Print[ "    Exact Value             : ", NIntegrate[ f[x],{x,-6.5,6.5} ] ];Print[ "    LH Sum (n = ",n,")         : ", LHSum[ f, {-6.5,6.5}, n ] //N ];Print[ "    RH Sum (n = ",n,")         : ", RHSum[ f, {-6.5,6.5}, n ] //N ];Print[ "    Trapezoidal Sum (n = ",n,"): ", t=Trap [ f, {-6.5,6.5}, n ] //N ];Print[ "    Midpoint Sum (n = ",n,")   : ", m=MidPt[ f, {-6.5,6.5}, n ] //N ];Print[ "    Simpson Sum (n  = ",n,")   : ", (2*m+t)/3 //N ];`

*)

(* Sample output:

Check Values for Numerical Integration:

Integration of Sin[Pi (6 + x)/17] on (-6.5, 6.5 ):

Exact Value : 9.03373

LH Sum (n = 10) : 8.44991

RH Sum (n = 10) : 9.53057

Trapezoidal Sum (n = 10): 8.99024

Midpoint Sum (n = 10) : 9.05549

Simpson Sum (n = 10) : 9.03374

*)

(9/12/96)jac