Program TRAP
Output: Trapezoidal rule approximation to
Z
b
a
f(x) dx.
Input: f(x) as Y
1
, A as a, B as b, N as n
:Disp “A”
:Input A
:Disp “B”
:Input B
:Disp “N”
:Input N
:0→S
:(B-A)/N→H
:A → X
:Y
1
→ U
:B → X
:Y
1
→ V
:A + H → X
:Lbl 1
:S+Y
1
→S
:X + H → X
:If X < B − .5H
:Goto 1
:(.5U + S + .5V )H → T
:Disp “T =”
:Disp T
:Stop
VOL
Output: The Riemann sums
n
X
i,j=1
f(x
i
, y
j
)hw where h = (b − a)/n, w = (d − c)/n
with (x
i
, y
j
) taken first at the lower left-hand corner
of each subrectangle and then at the upper right-hand
corner. The function z = f(x, y) is defined on the rect-
angle D = {(x, y) : A ≤ x ≤ B, C ≤ y ≤ D }. The
program pauses after the first Riemann sum. Press RE-
TURN to obtain the second sum.
Input: f(x, y) as Y
1
, A as a, B as b, C as c, D as d, N
as n.
:Disp “A =”
:Input A
:Disp “B =”
:Input B
:Disp “C =”
:Input C
:Disp “D =”
:Input D
:Disp “N =”
:Input N
:0 → S
:(B − A)/N → H
:(D −C)/N → W
:A + H → X
:C + W → Y
:Lbl 1
:S + Y
1
→ S
:Y + W → Y
:If Y < D − .5 ∗W
:Goto 1
:C + W → Y
:X + H → X
:If X < B − .5 ∗ H
:Goto 1
:S → L
:A → X
:C → Y
:Lbl 2
:Y
1
+ L → L
:Y + W → Y
:If Y < D − .5 ∗W
:Goto 2
:A + H → X
:C → Y
:Lbl 3
:Y
1
+ L → L
:X + H → X
:If X < B − .5 ∗ H
:Goto 3
:L ∗ H ∗W → L
:Disp L
:Pause
:S → R
:A + H → X
:D → Y
:Lbl 4
:R + Y
1
→ R
:X + H → X
:If X < B + .5 ∗ H
:Goto 4
:B → X
:C + W → Y
:Lbl 5
:R + Y
1
→ R
:Y + W → Y
:If Y < D − .5 ∗W
:Goto 5
:R ∗ H ∗ W → R
:Disp R
57