Monday, November 21, 2011

HP 15C Programming Tutorial - Part 8: The Summation Program

Sum

This program computes the sum:

R0 = ∑( X = R1, R2, f(R1))

where:
R0 = the sum
R1 = starting value, which gets updated at each step. R1 is also used as the variable for f(X)
R2 = finishing value

The program uses three labels:

Label A: The main routine. Press [ f ] [ √ ] (A) to run the routine.
Label 1: A subroutine of Label A. This is the loop.
Label 0: Where the function f(X) is stored. Assume that R1 is loaded on the X register when programming the function.

The program presented here will allow the user to enter starting and ending values beyond 999. You can shorten the program by the use of ISG, however, the ending value would be restricted to 999.

Program Listing

Key Code			Keys
001 42 21 11 LBL A
002 0 0
003 44 0 STO 0 * Initializes R0
004 33 R ↓
005 44 2 STO 2 * ending value
006 33 R ↓
007 44 1 STO 1 * starting value
008 42 21 1 LBL 1 * loop starts here
009 45 1 RCL 1
010 32 0 GSB 0
011 44 40 0 STO+ 0 * update total
012 1 1
013 44 40 1 STO+ 1 * update starting value
014 45 2 RCL 2
015 45 1 RCL 1
016 43 10 x≤y * is R1 ≤ R2?
017 22 1 GTO 1
018 45 0 RCL 0
019 43 32 RTN


Instructions:
1. Enter the main program (Labels A and 1).
2. Enter the function f(R1) (Label 0).
3. In run mode, enter the starting value and press [ENTER]
4. Enter the ending value and press [ f ] [ √ ] (A)
5. The sum is calculated and displayed.

Remember: Always finish the function with the RTN command ([ g ] [GSB] (RTN)).

Example 1

Find the sum ∑(X = 1, 50, X). In other words, what is the sum of the integers from 1 to 50?

Program listing for f(R1):

Key Code		Key
001 42 21 0 LBL 0
002 43 32 RTN


Surprised? Remember we already have a copy of R1 in the main program when the instruction GSB 0 is encountered.

1 [ENTER] 50 [ f ] [ √ ] (A)

Result: 1275


Example 2

Find the sum ∑(X = 1, 150, 1/(X^2 + X)).

1/(X^2 + X) can be rewritten as:

(X^2 + X)^-1

((X + 1) X)^-1

We will use the last form for our function.

A Way to Clear Label 0

Clear Label 0 is necessary. In Run mode, press [GTO] [ 0 ]. Then press [ g ] [R/S] (P/R) to enter program mode. Press [SST] until the key codes "43 32" is encountered. Press the backspace button the number of times you pressed [SST]. You should see the key code "42, 21, 0". You are ready to enter the new function.

Program Listing for f(R1):
Key Code		Key
001 42 21 0 LBL 0
002 36 ENTER
003 1 1
004 40 +
005 20 ×
006 15 1/x
007 43 32 RTN


To finish:

1 [ENTER] 150 [ f ] [ √ ] (A)

Result: ≈ 0.9934

That concludes Part 8 of our tutorial. Until next time,

Eddie




This tutorial is property of Edward Shore. © 2011