Tuesday, May 24, 2011

Polynomial Derivative and Integral for HP 35S

This program calculates the coefficients of the derivative and the integral of a polynomial. The maximum degree of the polynomial can be is seven.

The variables of the polynomial are:

H x^7 + G x^6 + F x^5 + E x^4 + D x^3 + C x^2 + B x + A


1. Load the Polynomial. Press XEQ P001, enter the degree of the polynomial and load the coefficients from the highest degree to the constant.

2. Operation. To find the derivative, press XEQ P033. For an indefinite integral, press XEQ P083.


' Start of program
' Messages in quotes can be typed by pressing the EQN key. Each letter is typed as so: RCL letter .
' To set/clear flag 10 press Gold Shift FLAGS. 1 for Set, 2 for Clear. "Type" 10 by pressing the decimal point followed by the zero.

P001 LBL P
P002 SF 10
P004 PSE
P005 CF 10
P007 1
P008 +
P009 STO M
P010 SF 10
P011 "X^"
P012 PSE
P013 RCL M
P014 1
P015 -
P016 PSE
P017 1
P018 +
P019 +/-
P020 STO I
P021 "COEF"
P022 PSE
P023 CF 10
P024 RCL(I)

' STOP is entered by pressing R/S

P026 STO(I)
P027 DSE M
P028 GTO P010
P029 SF 10
P030 "DONE"
P031 CF 10
P032 RTN

' Derivative Routine

P033 1
P034 RCL+ N
P035 STO M

' Calculate the coefficients

P036 RCL M
P037 +/-
P038 STO I
P039 +/-
P040 -1
P041 RCL+ M
P042 STOx(I)
P043 DSE M
P044 GTO P036

' Shift the coefficients to the appropriate slots

P045 2
P046 1
P047 RCL+ N
P048 1000
P049 ÷
P050 +
P051 STO M

P052 RCL M
P053 IP
P054 +/-
P055 STO I
P056 RCL(I)
P057 1
P058 STO+ I
P059 R↓
P060 STO(I)
P061 ISG M
P062 GTO P052

P063 RCL N
P064 +/-
P065 1
P066 -
P067 STO I
P068 0
P069 STO(I)

' Display Routine

P070 RCL N
P071 STO M

P072 RCL M
P073 +/-
P074 STO I
P075 VIEW(I)
P076 DSE M
P077 GTO P072

P078 SF 10
P079 "DONE"
P080 PSE
P081 CF 10
P082 RTN

' Integral Routine

' Calculate the coefficients

P083 1
P084 RCL +N
P085 STO M

P086 RCL M
P087 +/-
P088 STO I
P089 +/-
P090 1/x
P091 STOx(I)
P092 DSE M
P093 GTO P086

' Shift the coefficients into the proper place
' The constant term is assigned 0 (technically, it is an arbitrary constant)

P094 1
P095 RCL+ N
P096 STO M

P097 RCL M
P098 +/-
P099 STO I
P100 1
P101 -
P102 RCL(I)
P103 x<>y
P104 STO I
P105 x<>y
P106 STO(I)
P107 DSE M
P108 GTO P097

P109 0
P110 STO A

' Display Routine

P111 RCL N
P112 2
P113 +
P114 STO M

P115 RCL M
P116 +/-
P117 STO I
P118 VIEW(I)
P119 DSE M
P120 GTO P115

P121 SF 10
P122 "DONE"
P123 PSE
P124 CF 10
P125 RTN

' End of Program

Memory: LN=433


Find the derivative and integral of f(x) = x^3 - 2x^2 + 1

1. XEQ P001
2. Enter 3 at DEGREE prompt, then press R/S
3. "X^3" : 1, R/S
4. "X^2" : -2, R/S
5. "X^1" : 0, R/S
6. "X^0" : 1, R/S
7. XEQ P033. Press R/S after each coefficient.

Then f'(x) = 3x^2 - 4x

For the integral:
1. XEQ P001
2. Enter 3 at DEGREE prompt, then press R/S
3. "X^3" : 1, R/S
4. "X^2" : -2, R/S
5. "X^1" : 0, R/S
6. "X^0" : 1, R/S
7. Press XEQ P083.

Then the integral is .25x^4 - .6666666667x^3 + x

Recall that the variables of the polynomial are:

H x^7 + G x^6 + F x^5 + E x^4 + D x^3 + C x^2 + B x + A

Sunday, May 15, 2011

Complex Number Operations (Part II)

For all the calculations, let z = a + bi

Assume your calculator is in Radians Mode. The algorithms are suggested.

Exponential of a Complex Number

e^z = e^a (cos b + i sin b)

This calculation is fairly easy because it is relatively straight forward.

A sample RPN (Reverse Polish Notation) routine would look like:

With b on the y stack and a on the x stack:

STO 0 (or whatever register or variable you want as temporary storage)
x< >y (swap)
x< >y (swap)

Result: real(e^z) is on the x stack, while imag(e^z) is on the y stack

Natural Logarithm of a Complex Number

ln z = ln r + i θ

where r = √(x^2 + y^2) and θ = arctan(y/x)

Casio fx-115ES:
ln(abs(a + bi)) + i arg(a + bi)

Sharp EL-W516:
ln(abs(a + bi)) + i tan^-1(b÷a)

TI-36X Pro:
ln(abs(a + bi)) + i angle(a + bi)

With b on the y stack and a on the x stack, a sample RPN routine mat look like this:
→P (Rectangular to Polar conversion)

Argument vs Arctangent (tan^-1)

Bear in mind that the argument (often labeled arg or angle for TI calulators) and the arctangent functions returns different angles. However the angles are accurate, recall the identity tan(x ± π) = tan(x).

Ranges of Calculator Functions:

angle/arg returns the angle between -π/2 and π/2 (-90° and 90°)

tan^-1 returns the angle between 0 and π (0° and 180°)

Sine of a Complex Number

There are two general formulas to calculate the sine of a complex number

(1) sin z = sin a cosh b + i cos a sinh b

(2) sin z = (e^iz - e^-iz)/(2i)

Formula (2) is good if your calculator can handle e^z. For the non-graphing calculators, I recommend using formula (1).

A sample RPN routine looks like this:

With b on the y stack and a on the x stack:

STO 0 (or A)
x< >y
STO 1 (or B)
RCL 1 (or B)
RCL 0 (or A)

Cosine of a Complex Number

Calculating the cosine of a complex number is similar:

(1) cos z = cos a cosh b - i sin a sinh b

(2) cos z = (e^iz + e^-iz)/2

A sample RPN routine looks like this:

With b on the y stack and a on the x stack:

STO 0 (or A)
CHS (+/-)
x< >y
STO 1 (or B)
RCL 1 (or B)
RCL 0 (or A)

The formulas are presented for completion.

ArcSine and ArcCosine

These formulas are presented for completion.

sin^-1 z = -i ln( i z ± √(1 - z^2))

cos^-1 z = -i ln(z ± i√(1 - z^2))

The principle angle is given by using +.


Complex Power of a Complex Number

Let z and w be complex numbers, then:

z^w = e^(w ln z)

(HP 41C Math Pac)

Sunday, May 8, 2011

Complex Number Operations


Complex numbers have been a recurring feature on scientific calculators, and for most calculators, the calculations we can readily do on them have been limited - particularly on non-graphing scientific calculators.

If your non graphing calculator has complex number calculations, chances are this is the limit of what the calculator can do:

* Arithmetic (+, -, x, ÷)
* Square, but not square root
* Cube, but not cube root
* Rectangular/Polar conversions
* A separate function for magnitude, angle, real, and imaginary part extraction

The point of this blog is to give you some tricks to extend the calculation of complex numbers:

* Powers and principal root
* Exponential and Natural Logarithm (next week)
* Sine and Cosine (next week)

For any calculators using reverse polish notation (RPN), such the HP 35S, HP 50g, HP 15c, or even apps such as the GO-25 SCI RPN; I will present a "generalized" routine that you can use to perform the calculations. Please adjust as necessary what how your calculator works best.


HP 41C Math Pac, Hewlett Packard, 1979

Square of a Complex Number

By simple algebra, we can calculate the square of a complex number as:

(a + bi)^2 = (a^2 - b^2) + 2abi

Remember that i = √-1, so i^2 = -1.

Thankfully, scientific calculators allow this calculation simply by typing in (a + bi)^2.

For calculators using RPN, try this routine: starting with b stored in Register 2 (or letter if you have a HP 35S, 48, 50g...family) and a stored in Register 1

X (imaginary part)
- (real part)

Power (and Principal Root) of a Complex Number

By De Moive's Theorem, we can calculate the power of any complex number as:

(a + bi)^n = r^n (cos (nθ) + i sin (nθ))

Where: r = a^2 + b^2, θ = arctan (b/a), and n is a real number

Note abs(a + bi) = r and arg(a + bi) = θ (arg is named angle in the TI calculators)

On calculators such as the Casio fx-115ES, TI-36X Pro, and the Sharp EL-W516; unless n = 2 or n = 3, straight input will cause an error message. Use the above formula as the alternate. The nice thing of both the fx-115 and the TI-36X Pro is that we can store complex numbers to variables.

So the key strokes for the fx-115ES would look somewhat like this:
a + bi SHIFT STO A

Then input:
Abs(A)^(n) × (cos(n arg(A))+ i sin(n arg(A)))

For the TI-36X Pro:
a + bi sto→x [x y t z a b c d key]

Then input:
Abs(A)^(n) × (cos(n angle(A))+ i sin(n angle(A)))

For the EL-W516, complex numbers cannot be stored to variables. In addition, there is angle/argument function.

abs(a + bi)^n × (cos(n tan^-1(b ÷ a)) + i sin(n tan^-1(b ÷ a)))

A Sample RPN Routine for (a + bi)^n
Let a be stored in Register 1
Let b be stored in Register 2
Let n be stored in Register 3

STO 0. (temporary register)

Swiss Micros DM41X and Casio fx-CG 50: Minor Head Loss

Swiss Micros   DM41X and Casio fx-CG 50: Minor Head Loss Introduction When a fluid, such as water, is flowing in a pipe system, ene...