RIEMANN SUMS

/* User input comes first */
f: sin(x)  $/* Function f(x) */
a: 0   $/* Interval upper bound */
b: %pi   $/* Interval lower bound */
N: 20   $/* Number of iterations */
opt: 2   $/* 0-Right endpoints, 1-Midpoints, 2-Right endpoints */
/* End of user input */

h: float( b - a ) / N$
t: float( a + opt*h/2 )$
sum: 0.0$
for i: 1 thru N do
(   sum: sum + ev( f, numer, x=t ),
    t: t + h
)$
print( "Riemann sum is: ", h*sum )$

The code below implements the same procedure as above,
but additionaly it gives the symbolic form of the Riemann sum.

/* User input comes first */
f(x):= x-x^3   $/* Define function */
a: 0   $/* Left endpoint */
b: 1   $/* Right endpoint */
N: 20   $/* Number of subintervals */
opt: 2   $/* 0 for left endpts, 1 for midpts, 2 for right endpts */
/* End of user input */

h: (b-a)/N$
c_k: ratsimp(a + ( 2 * (i-1) + opt ) * h/2)$
ans1: 'sum( f(c_k)*h, i, 1, N )$
ans2: ev(%, sum)$
ans3: float(%)$
print( ans1, " = ", ans2, " = ", ans3 )$