Wednesday, November 16, 2011

HP 15C Programming Tutorial - Part 5: Subroutines

Subroutines

A subroutine is a set a of repeated instructions that is called within a main program. The subroutine is keyed in only once which saves memory. There is a return command at the end of each subroutine which allows the calculator to return to the point where the subroutine is called.

For example, let's program the path as someone enters a house after getting home from work. Typically, the person has to open doors many times. Let this be the subroutine. A program diagram would look like this:

Person Enters House

Subroutine: Open Door

Enter Living Room

Go to the Bedroom

Subroutine: Open Door

Enter Bedroom

Go to Closet

Subroutine: Open Door

Put down briefcase, purse, and/or backpack

Relax - END

Subroutine: Open Door

Is the door locked?
Yes: Use appropriate key to unlock it
No: Go onto the next step

Use hand to turn the knob

Push the door

Walk in to the next room

RETURN


Subroutines on the HP 15C

In Programming Mode, press the [GSB] key (3rd row, 2nd key) followed by the label (0-9, .0-.9, A-E). According to the HP 15C manual, GSB stands for go to subroutine.

Subroutines can call other subroutines, subroutines of subroutines are called nested subroutines. On the HP 15C, subroutines can be nested seven levels deep.

Part 5 contains two programs to illustrate the use of subroutines. In the second program presented, we will make use of a subroutine help to program the Quadratic Formula.

Using a Subroutine

Let f(x) = cos^-1 (sin 2x/π) * sin^-1 (cos (2x/π)^2) * (2x/π)^3 ; where x is in radians. Calculate f(1), f(2), and f(3).

Note that the expression 2x/π repeats. Let's use this as a subroutine.

In this example, we will label the main program C and use Label 2 as the subroutine.

Program Listing:


Key Code Key
001 42 21 13 LBL C
002 43 8 RAD
003 44 0 STO 0
004 32 2 GSB 2
005 23 SIN
006 43 24 COS^-1
007 45 0 RCL 0
008 32 2 GSB 2
009 43 11 x^2
010 24 COS
011 43 23 SIN^-1
012 20 ×
013 45 0 RCL 0
014 32 2 GSB 2
015 3 3
016 14 y^x
017 20 ×
018 45 0 RCL 0
019 32 2 GSB 2
020 10 ÷
021 43 32 RTN

022 42 21 2 LBL 2 *subroutine starts
023 2 2
024 20 ×
025 43 26 π
026 10 ÷
027 43 32 RTN


Instructions

Enter x. Press [ f ] [10^x] (C).

Examples

f(1) ≈ 0.4413
f(2) ≈ -0.0243
f(3) ≈ -1.3169

Quadratic Formula

This program find the roots the quadratic equation:

ax^2 + bx + c = 0

Let's assign memory registers R1, R2, and R3 as follows:

R1 = a
R2 = b
R3 = c

Values for R1, R2, and R3 are stored before program execution.

The other memory registers used in this program are R0, R4, and R5, where:

R0 = discriminant = b^2 - 4ac

If R0 < 0, then
R4 = the real part of the complex roots x ± yi
R5 = the imaginary part of the complex roots x ± yi

If R0 ≥ 0 then
R4 = one of the real roots
R5 = one of the real roots

Program Instructions


Key Code Key
001 42 21 11 LBL A
002 45 2 RCL 2
003 43 11 x^2
004 4 4
005 45 20 1 RCL × 1
006 45 20 3 RCL × 3
007 30 -
008 44 0 STO 0
009 31 R/S
010 43 30 2 x<0? * TEST 2
011 22 0 GTO 0 * R0 < 0 - complex roots
012 4 4 * real roots
013 10 ÷
014 45 1 RCL 1
015 43 11 x^2
016 10 ÷
017 11 √
018 32 1 GSB 1
019 34 x<>y
020 40 +
021 44 4 STO 4
022 31 R/S
023 43 36 LSTx
024 2 2
025 20 ×
026 30 -
027 44 5 STO 5
028 43 32 RTN

029 42 21 0 LBL 0 * complex roots
030 32 1 GSB 1
031 44 4 STO 4
032 31 R/S
033 45 0 RCL 0
034 43 16 ABS
035 11 √
036 2 2
037 10 ÷
038 45 10 1 RCL÷ 1
039 44 5 STO 5
040 43 32 RTN

041 42 21 1 LBL 1 * subroutine starts here
042 45 2 RCL 2
043 16 CHS
044 2 2
045 10 ÷
046 45 10 1 RCL÷ 1
047 43 32 RTN


ax^2 + bx + c = 0

Instructions:
1. Store a in R0, b in R1, and c in R2.
2. Press [ f ] [ √ ] (A).
3. The discriminant is displayed (R0).
4. Press [R/S] displays R4.
5. Press [R/S] displays R5.

If R0 ≥ 0, R4 and R5 are the real roots of the quadratic equation.

If R0<0, R4 is the real part of the complex root, and R5 is ±imaginary part of the complex root.

Examples:

1. x^2 + 4x + 6
R1 = 1, R2 = 4, R3 = 6
Result: R0 = -8, R4 = -2, R5 ≈ 1.4142
The roots are -2 ± √2 i

2. x^2 - 5x + 3
R1 = 1, R2 = -5, R3 = 3
Result: R0 = 13, R5 ≈ 0.6972, R4 ≈ 4.3028
The roots are approximately 0.6972 and 4.3028.

I hope found Part 5 helpful, and I'll see you for Part 6! Eddie



This tutorial is property of Edward Shore. © 2011

No comments:

Post a Comment

HP Prime and Casio fx-CG50: Trapezoid Midsegment, Height, Area

HP Prime and Casio fx-CG50:  Trapezoid Midsegment, Height, Area The program TRAPEZ calculates the following: Midsegment leng...