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