Trapezoid Rule

/* User input comes first */
f: 1/x  $/* Function f(x) */
a: 1   $/* Interval upper bound */
b: 2   $/* Interval lower bound */
N: 8   $/* Number of subintervals */
/* End of user input */

h: float( b - a ) / N$
sum: ( ev( f, numer, x=a ) + ev( f, numer, x=b ) ) / 2.0$
t: float( a )$
for i: 1 thru N-1 do
(   t: t + h,
    sum: sum + ev( f, numer, x=t )
)$
print( "Trapezoid rule approximation is: ", h*sum )$

Trapezoid Rule with End Correction

/* User input comes first */
f: 1/x  $/* Function f(x) */
a: 1   $/* Interval upper bound */
b: 2   $/* Interval lower bound */
N: 8   $/* Number of subintervals */
/* End of user input */

h: float( b - a ) / N$
sum: ( ev( f, numer, x=a ) + ev( f, numer, x=b ) ) / 2.0$
t: float( a )$
for i: 1 thru N-1 do
(   t: t + h,
    sum: sum + ev( f, numer, x=t )
)$
fp: diff( f, x )$
sum: sum + h * ( ev( fp, numer, x=a ) - ev( fp, numer, x=b ) ) / 12.0$
print( "Improved trapezoid rule approximation is: ", h*sum )$