RIEMANN

In review

This page runs a BASIC program that computes a Riemann sum for

\[ \int_1^3 \sqrt{1 + x^3}\, dx. \]

The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to run it again; Reset brings back the original program.

The interval from a = 1 to b = 3 is split into numberofsteps pieces of width deltax. On each piece, deltaS = fnf(x) * deltax is the area of a rectangle whose height is the value of the function at the left end of the piece. The program adds these up in accumulation and prints each rectangle’s area next to the running total. With 4 steps the sum is about 5.2916.

This integral has no formula in terms of familiar functions, so the Riemann sum is the way to find its value, which is about 6.22996. Because \(\sqrt{1 + x^3}\) is increasing on \([1, 3]\), each left-endpoint rectangle sits below the curve and the sum comes out too small. Try numberofsteps = 40, 400, and 4000 and watch the last total climb toward 6.22996. (For large values the output gets long; you may want to move the print out of the loop so it prints just the final total.)

QBasic lets a FUNCTION be written at the end of the program, after the code that uses it. Python needs a function to be defined before it is called, so in the Python the def comes first, and return gives back the value that BASIC sets with fnf = .... SQR becomes math.sqrt, and ^ becomes **.

Notes from the textbook

Add notes here.

 1  DEFDBL A-Z
 2  a = 1
 3  b = 3
 4  numberofsteps = 4
 5  deltax = (b - a) / numberofsteps
 6  x = a
 7  accumulation = 0
 8  FOR k = 1 TO numberofsteps
 9          deltaS = fnf(x) * deltax
10          accumulation = accumulation + deltaS
11          x = x + deltax
12          PRINT deltaS, accumulation
13  NEXT k
14  FUNCTION fnf (x)
15          fnf = SQR(1 + x ^ 3)
16  END FUNCTION

Output

Everything the program prints appears here, one line for each print(...), like BASIC's PRINT. Numbers printed on the same line are lined up in columns.

Loading Python. The first run takes a few seconds.

Python code

Press Run (or Ctrl/⌘ + Enter) to run the program again. Press Esc, then Tab, to leave the editor.

Loading Python…