Sunday, December 18, 2011

HP 15C Programming Tutorial - Part 15: Memory Exchange (Area of a Polygon)

Memory Exchange

We will introduce another powerful feature of the HP 15C: memory exchange. This will allow the user to exchange the contents of the X register with a designated memory register.

Key strokes: [ f ] [ 4 ] (x > <)

Example: Store 2 in memory register 5. Call the constant π and exchange it with the contents of memory register 5.

Key strokes:
2 [STO] 5 (Display: 2.0000)
[ g ] [EEX] ( π ) (Display: 3.1416)
[ f ] [ 4 ] (x > <) 5 (Display: 2.0000) - now π in in R5
Check: [RCL] [ 5 ] (Display: 3.1416)

We will use this in a program.

Area of a Polygon with Transverse Coordinates

This program calculates the area of a polygon which vertices are given by (x,y) coordinates by the approximation:

R ≈ 0.5 × abs(∑(y_i * x_i+1 - y_i+1 * x_i, i = 0, n-1))

In geographic terms:


| y > 0, North
|
|
x< 0, |
West |
---------------------------
| x > 0, East
|
|
|
| y < 0, South


Labels Used:
LBL A - Initialize the program and enters the initial coordinates
LBL B - Enters additional coordinates
LBL C - Calculates area

Memory Registers Used:
R0 = Area
R1 = x_i
R2 = y_i
R3 = x_i+1
R4 = y_i+1
R5 = temporary use
R6 = temporary use

Program:

KEY CODES KEYS
001 42 21 11 LBL A
002 0 0
003 44 5 STO 5
004 44 6 STO 6
005 33 R ↓
006 44 1 STO 1
007 34 x<>y
008 44 2 STO 2
009 2 2
010 43 32 RTN
011 42 21 12 LBL B
012 44 3 STO 3
013 34 x<>y
014 44 4 STO 4
015 34 x<>y
016 45 20 2 RCLx 2
017 44 40 6 STO+ 6
018 45 4 RCL 4
019 45 20 1 RCLx 1
020 44 40 5 STO+ 5
021 45 3 RCL 3
022 42 4 1 x<>1 * Exchange X with R1
023 45 4 RCL 4
024 42 4 2 x<>2 * Exchange X with R2
025 43 32 RTN
026 42 21 13 LBL C
027 45 5 RCL 5
028 45 30 6 RCL- 6
029 43 16 ABS
030 2 2
031 10 ÷
032 44 0 STO 0
033 43 32 RTN


Instructions:

1. Enter the initial y coordinate, hit [ENTER], the initial x coordinate, and press [ f ] [ √ ] (A).
2. For the additional points, enter the y coordinate, press [ENTER], enter the x coordinate, and the [ f ] [e^x] (B).
3. To calculate the area, press [ f ] [10^x] (C).

Example:

Find the area of the polygon with the following vertices:
(0, 0)
(4, 4)
(0, 8)
(-2, 8)
(-3, -1)

Key Strokes:

0 [ENTER] 0 [ f ] [ √ ] (A)
4 [ENTER] 4 [ f ] [e^x] (B)
8 [ENTER] 0 [ f ] [e^x] (B)
8 [ENTER] 2 [CHS] [ f ] [e^x] (B)
1 [CHS] [ENTER] 3 [CHS] [ f ] [e^x] (B)
[ f ] [10^x] (C)

Area = 37

I hope you find this program useful and fun. Until next time,

Eddie


This tutorial is property of Edward Shore. © 2011

HP 15C Programming Tutorial - Part 16: Solving Multiple Input Equations (TVM Program)

Solving Multiple Input-Single Output Equations

In Part 16, we will use the HP 15C's solve feature to solve multiple input-single output (MISO) equations.

This tutorial will be using a technique introduced by Karl Schneider. Please click on this link to read about it.

It involves using a program to write the equation f(x1, x2, x3, ...) = 0. The equation begins with the instructions: LBL #, STO(i); where # is the label letter/number of your choice. The next step is to put the variable number in memory register I. Finally, execute the solve function. Let's see this in action.

Volume of a Cylinder

In this first program will turn the volume of a cylinder into a MISO solver. The equation is:

V = π r^2 h

The first step is to make one side of the equation zero.

0 = π r^2 h - V

We'll have to designate memory registers for each of the variables. Let's use:

R1 = r
R2 = h
R3 = V

The equation can be seen as:

0 = π R1^2 R2 - R3

Next enter the program:


Key Codes Keys
001 42 21 11 LBL A
002 44 25 STO(i)
003 43 26 π
004 45 1 RCL 1
005 43 11 x^2
006 20 ×
007 45 20 2 RCLx 2
008 45 3 RCL 3
009 30 -
010 43 32 RTN


Now we can use the program as MISO solver.

Example 1: Given V = 1,000 and h = 10. Find r.

The variable r corresponds to R1. This means we store 1 in memory register I. Give an initial interval (0, 1000).

Key Strokes:
1000 [STO] [ 3 ]
10 [STO] [ 2 ]
1 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: r ≈ 5.6419

Example 2: Given V = 2,498.65 and r = 39.43. Find h. Use an initial interval (0, 1000). We are looking for h, so store 2 in memory register I.

Key Strokes:
2498.65 [STO] [ 3 ]
39.43 [STO] [ 1 ]
2 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: h ≈ 0.5116

Time Value of Money

This program uses the solver capabilities to solve common financial programs.

The equation:

0 = PV + PMT × ((1 - (1 + I)^-N) / I) + FV × (1 + I)^-N

We will designate the following registers for the variables:
R1 = N (number of periods)
R2 = I (periodic interest rate, entered as a decimal)
R3 = PV (present value)
R4 = PMT (payment)
R5 = FV (future value)

End-of-period payments is assumed.

We can rewrite the equation as this:

0 = R3 + R4 × ((1 - (1 + R2)^-R1) / R2) + R5 × (1 + R2)^-R1

Program:


Key Codes Keys
001 42 21 11 LBL A
002 44 24 STO(i)
003 1 1
004 45 40 2 RCL+2
005 45 1 RCL 1
006 16 CHS
007 14 y^x
008 16 CHS
009 1 1
010 40 +
011 45 10 2 RCL÷ 2
012 45 20 4 RCLx 4
013 1 1
014 45 40 2 RCL+ 2
015 45 1 RCL 1
016 16 CHS
017 14 y^x
018 45 20 5 RCLx 5
019 40 +
020 45 40 3 RCL+ 3
021 43 32 RTN


Example 1: Given N = 360, I = 5%/12, PV = 200,000, and FV = 0, find PMT. PMT refers to memory register 4. (Store 4 in RI). Give an initial interval of (0, 1000).

Key Strokes:
360 [STO] [ 1 ]
.05 [ENTER] 12 [ ÷ ] [STO] [ 2 ]
200000 [STO] [ 3 ]
0 [STO] [ 5 ]
4 [STO] [TAN] ( I )
0 [ENTER] 1000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: PMT = -1073.6432 ($1,073.64)

Example 2: Given N = 48, PV = 25,000, PMT = -600, and FV = 0, solve for I. The variable I refers to memory register 2. (Store 2 in RI). Give an initial interval of (0.1, 1).

Caution: When solving for interest, do not use 0 as an endpoint of an interval, for this will cause an "Error 0" condition.

Key Strokes:
48 [STO] [ 1 ]
25000 [STO] [ 3 ]
600 [CHS] [STO] [ 4 ]
0 [STO] [ 5 ]
2 [STO] [TAN] ( I )
0.1 [ENTER] 1 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)
(to find the annual rate) 12 [ × ]

Answer: I = .0059 (Periodic Rate 0.59%), .0712 (Annual Rate 7.12%)

Example 3: Given N = 10, I = .03, PV = -1000, PMT = -250, solve for FV. This time we are solving for variable # 5 (FV). Use an initial interval (1000, 5000).

Key Strokes:
10 [STO] [ 1 ]
.03 [STO] [ 2 ]
1000 [CHS] [STO] [ 3 ]
250 [CHS] [STO] [ 4 ]
5 [STO] [TAN] ( I )
1000 [ENTER] 5000 [ f ] [ ÷ ] (SOLVE) [ √ ] (A)

Answer: FV = 4209.8862 ($4,209.89)

Thank you for joining me for another session.

Source: Schneider, Karl. "SOLVE and INTEG on HP's RPN-based models" HP Articles. Museum of HP Calculators. October 2, 2005. http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/articles.cgi?read=556

Thank you Karl!!!




This tutorial is property of Edward Shore. © 2011

Sunday, December 11, 2011

HP 15C Programming Tutorial: Part 14 - Complex Numbers

Complex Numbers and the HP 15C

The HP 15C can operate with complex numbers. To use complex numbers, set flag 8 by pressing [ g ] [ 4 ] (SF). You will know that flag 8 is set when the display has a "C" indicator.

A complex number (a + bi) consists of two parts: its real part (a) and its imaginary part (b). Each part is dealt with separately. When the calculator is in complex mode, each of the four stack registers, X, Y, Z, and T, has a real part and a separate part for the imaginary part. The display only shows the real part of the X register - this is important to know.

The stack:


--------------------------------------------
real part of T | imaginary part of T
---------------------------------------------
real part of Z | imaginary part of Z
--------------------------------------------
real part of Y | imaginary part of Y
--------------------------------------------
real part of X | imaginary part of X
--------------------------------------------


Complex Number Basics

Complex Number Entry

Two ways to enter complex numbers, one use the I register, the other uses the Re<>Im function.

Way 1:

real part [ENTER] imaginary part [ f ] [TAN] (I)

Effects on the stack:

-------------------------------------------
T: contents previously held in Y
-------------------------------------------
Z: contents previously held in Y
-------------------------------------------
Y: contents previously held in X
-------------------------------------------
X: a + bi
------------------------------------------


The contents previously held in stack registers Z and T are lost.

If you want the stack registers Y, Z, and T to stay intact, use Way 2.

Way 2:

imaginary part [ f ] [ - ] (Re<>Im) real part

Effects on the stack:

------------------------------------------
T: contents previously held in T
------------------------------------------
Z: contents previously held in Z
------------------------------------------
Y: contents previously held in Y
------------------------------------------
X: a + bi
------------------------------------------


Entering Pure Imaginary Numbers

You can easily enter imaginary numbers by using the following key stroke sequence:

imaginary part [ f ] [ - ] (Re<>Im)

Viewing the Imaginary Part of a Complex Number

To view the imaginary part of a complex number without affecting anything, press [ f ] and hold [TAN] (I) to view the imaginary part. Letting go of the [TAN] key returns the display after about a half of second.

Hint: Keep in mind that [ f ] [ - ] (Re<>Im) switches the real and imaginary parts of the X register.

Conjugate of a Complex Number

The conjugate of a complex number a + bi is a - bi. Use this key sequence:

[ f ] [ - ] (Re<>Im) [CHS] [ f ] [ - ] (Re<>Im)

Negation of a Complex Number

Since we can only work with one part of a complex number at a time, simply pressing [CHS] is not enough. To negate a complex number, use the key sequence:

1 [CHS] [ × ]

Memory Registers and Complex Numbers

Each memory register can have only either the real part or the imaginary part of a complex number. Two memory registers have to be used to store a complete complex number. Furthermore, during programming, you will have construct the complex number from the two parts.

In the program presented in Part 14, five complex numbers will be stored in memory. We will need ten registers to store the real and imaginary parts for each complex number.

Trigonometry and Complex Numbers

According to the HP 15C manual, the calculator treats the number to be in Radians regardless of the angle setting when the trigonometric functions (SIN, COS, TAN, SIN^-1, COS^-1, TAN^-1) are executed.

The exception is the polar and rectangular conversions. The angle mode is taken into account. Only the X stack register is affected.

Square Roots of Negative Numbers

You will need to be in complex number mode to calculate square roots of negative numbers - otherwise "Error 0" occurs.

Conditional Tests with Complex Numbers

Let X = a + bi and Y = c + di.

There are only four conditional tests available with complex numbers:
[ g ] [ × ] (x = 0): Does a + bi = 0 + 0i?
[ g ] [ - ] (TEST) 0 (x ≠ 0): Does a + bi ≠ 0 + 0i?
[ g ] [ - ] (TEST) 5 (x = y): Does a + bi = c + di
[ g ] [ - ] (TEST) 6 (x ≠ y): Does a + bi ≠ c + di?

Leaving Complex Mode

Clear flag 8.

The Quadratic Equation with Complex Coefficients

The following equation solves the quadratic equation:

A × Z^2 + B × Z + C = 0

where A, B, C, and Z are complex numbers.

The roots are:

Z = ( -B ± √(B^2 - 4 A C)) / (2 A)

Memory Registers used:

R0 = real part of A
R1 = imaginary part of A
R2 = real part of B
R3 = imaginary part of B
R4 = real part of C
R5 = imaginary part of C
R6 = real part of Root 1
R7 = imaginary part of Root 1
R8 = real part of Root 2
R9 = imaginary part of Root 2

So:
A = R0 + R1 i
B = R2 + R3 i
C = R4 + R5 i
Root 1 = R6 + R7 i
Root 2 = R8 + R9 i

Labels used: A (Main), 0 and 1 (Subroutines)

Program Listing:


Key Codes Keys
001 42 21 11 LBL A
002 43 4 8 SF 8 * Set complex mode
003 32 0 GSB 0
004 32 1 GSB 1
005 44 6 STO 6
006 31 R/S
007 42 30 Re<>Im
008 44 7 STO 7
009 31 R/S
010 32 0 GSB 0
011 1 1
012 16 CHS
013 20 ×
014 32 1 GSB 1
015 44 8 STO 8
016 31 R/S
017 42 30 Re<>Im
018 44 9 STO 9
019 31 R/S
020 42 30 Re<>Im
021 43 32 RTN
022 42 21 0 LBL 0 * Discriminant Subroutine
023 45 0 RCL 0
024 45 1 RCL 1
025 42 25 I * [ f ] [TAN] (I)
026 45 4 RCL 4
027 45 5 RCL 5
028 42 25 I
029 20 ×
030 4 4
031 16 CHS
032 20 ×
033 45 2 RCL 2
034 45 3 RCL 3
035 42 25 I
036 43 11 x^2
037 40 +
038 11 √
039 43 32 RTN
040 42 21 1 LBL 1 * B^2/2A Subroutine
041 45 2 RCL 2
042 16 CHS
043 45 3 RCL 3
044 16 CHS
045 42 25 I
046 40 +
047 2 2
048 10 ÷
049 45 0 RCL 0
050 45 1 RCL 1
051 42 25 I
052 10 ÷
053 43 32 RTN


Instructions:

1. Store the real and imaginary parts of A, B, and C. See the above for the appropriate registers.
2. Press [ f ] [ √ ] (A)
3. The real part of Root 1 is displayed. Press [R/S].
4. The imaginary part of Root 1 is displayed. Press [R/S].
5. The real part of Root 2 is displayed. Press [R/S].
6. The imaginary part of Root 2 is displayed.

Example 1:

A = 2 + 3i
B = -3 - 4i
C = 2i

Registers:

R0 = 2
R1 = 3
R2 = -3
R3 = -4
R4 = 0
R5 = 2

Root 1 ≈ 0.2578 + 0.3769i
Root 2 ≈ 1.1268 - 0.4538i

Example 2:

A = -4 + 5i
B = 3
C = 2√2 - 3i

Registers:
R0 = -4
R1 = 5
R2 = 3
R3 = 0
R4 = 2√2 (2 [ENTER] [ √ ] [ × ])
R5 = -3

Root 1 ≈ -0.6500 + 0.1165i
Root 2 ≈ 0.9427+ 0.2493i

That is all for the tutorial. Until next time, happy programming! Eddie



This tutorial is property of Edward Shore. © 2011

The 12 Days of Christmas

Holiday Cheer!

So it is that time of the year: most of us are in a rush to purchase Christmas presents for our loved ones, perhaps taking advantage of the vast of amount of discounts stores are offering to entice us to buy. This is also the time where Christmas Carols are sung, even played 24/7 on some radio stations. Where I live (Azusa, CA which is about 25 miles east of Los Angeles, or Dodgertown for the baseball fans), I know of two stations that are all-Holiday music all the time until Christmas.

Chances are that you have heard about the "12 Days of Christmas" sung by about every recording artist that's ever lived (at least in America). The song was originally published around 1780 and came to the United States about 1910. The song tells about a very generous person give his/her true love presents for each day of Christmas. The days of Christmas starts on December 25 (or 26 for some cultures), and the giving lasts for 12 days.

How Many Presents?

How may presents did the generous lover give his/her true love? It depends on how the gifts are counted.

Take the second day for example:

On the second day of Christmas, my true love gave to me...
Two turtle doves and
A partridge in a pear tree.


Taking a literal count, we can say the true love received a total of three gifts: the doves AND another partridge in a pear tree.

However, some view each day as a summary of all the gifts after the new set of gifts were received. Hence, the true love received only the two doves because the true love was already given the partridge in a pear tree.

Some even count the partridge as one gift and the pear tree as another. For simplicity, I'll count the partridge in a pear tree as one gift.

The Math

To calculate the number of gifts given during the 12 Days of Christmas, it is only a matter of addition.

However, we can use the shortcut if we are adding consecutive numbers from 1 to N:

S = ∑(K = 1, N, K) = ((N + 1) × N) ÷ 2

How do we get this?

Let S represent the sum:

S = 1 + 2 + 3 + ... + (n-2) + (n-1) + n

Add S to both sides:

2 × S = 1 + 2 + 3 + ... + (n-2) + (n-1) + n + 1 + 2 + 3 + .... + (n-2) + (n-1) + n

Note that addition is communicative. That is for any two numbers a and b, a + b = b + a. This will allow us to rearrange the terms on the right side of the equation like so:

2 × S = 1 + n + 2 + (n-1) + 3 + (n-2) + .... + (n-2) + 3 + (n-1) + 2 + n + 1

Add each pair of terms on the right side. Note that 2 + (n-1) = n+1, 3 + (n-2) = n+1, and so on.

2 × S = (n+1) + (n+1) + (n+1) + .... + (n+1) + (n+1) + (n+1)

Observe that there are n terms of (n+1) on the right side.

2 × S = n × (n+1)

Divide both sides by 2 and we get:

S = (n × (n + 1)) / 2

We'll use this formula to help us count the number of presents.

Summary Count

If each day the lucky recipient merely recaps what he/she has received to that day, then the number of gifts the person gets is 78.

S = (12 × (12 + 1)) / 2 = (12 × 13) / 2 = 78 which consists of:


One partridge in a pear tree, (received on the first day)
Two turtle doves, (received on the second day)
Three French hens, (etc...)
Four calling birds,
Five gold rings,
Six geese-a-lying,
Seven Swans-a-swimming,
Eight Maids-a-milking,
Nine Ladies dancing,
Ten Lords-a-leaping,
Eleven Pipers pipping, and
Twelve Drummers drumming


Note there is no mention to what exactly the maids were milking but I digress.

Literal Count

A popular view to find the number of gifts is to make a literal count. That means each time the singer person sings about what he/she got from his/her true love actually was received. From this perspective, after the 12 Days of Christmas were over, the person actually got a total of 12 partridges in pear trees, along with 22 turtle doves, and so on. Hopefully the person has a big yard to fit all the pear trees, the turtle doves, a lake for all the geese, and many rooms to house the maids, ladies, lords, pipers, and drummers.

Let's calculate the total shall we?
Let S = number of gifts received that day and T = cumulative total of all the gifts received

First Day:
A Partridge in a Pear Tree

This is easy:
S = 1
T = 1

Second Day:
Two Turtle doves and
A Partridge in a pear tree

S = 1 + 2 = (2 × 3) / 2 = 3
T = 3 + 1 = 4 (1 from Day 1 and 3 from Day 2)

Third Day:
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 = (3 × 4) / 2 = 6
T = 4 + 6 = 10 (6 from Day 3 and 4 from the past two days)

Fourth Day:
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 = (4 × 5) / 2 = 10
T = 10 + 10 = 20

Fifth Day:
Five golden rings,
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 + 5 = (5 × 6) / 2 = 15
T = 20 + 15 = 35

Sixth Day:
Six geese-a-laying,
Five golden rings,
Four calling birds,
Three French hens,
Two Turtle doves, and
A Partridge in a pear tree

S = 1 + 2 + 3 + 4 + 5 + 6 = (6 × 7) / 2 = 21
T = 35 + 21 = 56

If the true love stopped here, the person has already accumulated a nice haul of 56 presents! But we know the true love kept going, so...

Seventh Day:
Seven Swans-a-swimming and everything else from the first six days

S = 1 + 2 + 3 + 4 + 5 + 6 + 7 = (7 × 8)/2 = 28
T = 56 + 28 = 84

Eighth Day:
Eight Maids-a-milking and everything else from the first seven days

S = 1 + ... + 8 = (8 × 9)/2 = 36
T = 84 + 36 = 120

This has got to cost the true love a fortune!

Ninth Day:
Nine Ladies dancing and everything else from the first eight days

S = 1 + ... + 9 = (9 × 10)/2 = 45
T = 120 + 45 = 165

Tenth Day:
Ten Lords-a-leaping and everything else from the first nine days

S = 1 + ... + 10 = (10 × 11)/2 = 55
T = 165 + 55 = 220

Eleventh Day:
Eleven Pipers pipping and everything else from the first ten days

S = 1 + ... + 11 = (11 × 12)/2 = 66
T = 220 + 66 = 286

Twelfth Day:
Twelve Drummers drumming and everything else from the first eleven days

S = 1 + ... + 12 = (12 × 13)/2 = 78
T = 286 + 78 = 364

The grand total of all the gifts were 364. I can only imagine that the last few days busy the person and the true love.





Here is a link that has the lyrics of "The Twelve Days of Christmas".

Twelve Days of Christmas

Happy Holidays everyone! Merry Christmas! May this month (and all months thereafter) find you peace, love, warmth, and gratitude!

Eddie

Retro Review: Texas Instruments TI-68

Retro Review:  Texas Instruments TI-68 Company:  Texas Instruments Years:  1989 - 2002 Type:  Scientific, Formula Programmin...