Tuesday, March 6, 2012

Spirals


All About Spirals

This blog will about spirals. Why? I thought it would be fun. And I like spirals.


Regular Spirals

Source: http://www.mathematische-basteleien.de/spiral.htm

Equations:
Rectangular
x^2 + y^2 = a^2 (arctan y/x)^2

Polar
r = a θ

Parametric
x = a t cos t
y = a t sin t


Finding the y-intercept and x-intercept, as shown above, of a spiral, assuming 0 ≤ θ ≤ 2 π

We can use the rectangular equation to find these points.

x^2 + y^2 = a^2 (arctan y/x)^2

Y-Intercept - Point (0, y)

y^2 = a^2 (arctan y/0)^2

Note:
1. y/0 is undefined
2. tan (π /2) is undefined. So is tan (n π / 2) where n is an integer.

Use π / 2 as a solution.

Then:

y^2 = a^2 * (π / 2)^2

Take the square root of both sides, since y>0, use the positive root.

y = a * π / 2

The y-intercept is (0, a * π / 2)

X-Intercept - Point (x, 0)

x^2 = a^2 (arctan 0)^2

Note tan(n π.)=0 where n is an integer. Let's use π. (n = 1)

x^2 = a^2 π^2

Since the x-intercept lies left of 0, select the negative square roof. Therefore:

x = -a π.

The x-intercept is (-a π, 0).

Area of a Spiral (0 ≤ θ ≤ 2 π)

Use the polar equation: r = a θ

Using the general polar integral:

A = ∫ (1/2 * r^2 d θ , lower limit, upper limit)

A = ∫ (1/2 * a^2 * θ^2 d θ , 0, 2 π)
= a^2 / 2 * (8 π^3 / 3 - 0)
= 4 a^2 π^3 / 3

Slope of a Spiral

Polar Form:

∂r/∂θ = a

Parametric Form:

∂x/∂t = ∂/∂t (a * t * cos t)
= ∂/∂t (a * t) * cos t + a * t * ∂/∂t (cos t)
= a * cos t - a * t * sin t
= a * (cos t - t * sin t)

∂y/∂t = ∂/∂t (a * t * sin t)
= ∂/∂t (a * t) * sin t + a * t * ∂/∂t (sin t)
= a * sin t + a * t * cos t
= a * (sin t + t * cos t)

Stretch Spirals

What if we can "stretch" spirals? We can if alter the parameters of the parametric form:

x(t) = a * t * cos t
y(t) = b * t * sin t

Where a ≠ b.

Two graphical examples are shown below.


Finding the Intercepts of a Stretched Spiral

I will assume that both a ≠ 0 and b ≠ 0.

Y-Intercept: Point (0, y)

0 = a * t * cos t
y = b * t * sin t

Working with the first equation:

0 = a * t * cos t
0 = t * cos t,

which implies that either t = 0 or cos t = 0

We know that cos (n π / 2) = 0 where n is an integer. Choose t = π / 2.

Then y = b * π / 2 * sin(π / 2) = b * π / 2

Then the y-intercept is (0, π / 2).

X-Intercept: Point (x, 0)

x = a * t * cos t
0 = b * t * sin t

Working with the second equation:
0 = b * t * sin t
0 = t * sin t

which implies that t = 0 or sin t = 0. We know that sin(n π) = 0 where n is an integer, let's choose n = 1 and then t = π.

Hence x = a * π * cos π = -a * π.

The x-intercept is (-a * π, 0)

Area of a Stretched Spiral

We can find the area using the following:

∫ y dx = ∫ y(t) d[x(t)]

Note that:
1. y(t) = b * t * sin t
2. d[x(t)] = a * (cos t - t * sin t) dt
3. y(t) d[x(t)] = a * b * (t * sin t * cos t - t^2 * sin^2 t)

Let Φ = (t * sin t * cos t - t^2 * sin^2 t)

The integration is split into two intervals: [0, π] and [π, 2 π]. Due to the direction of the spiral, the limits are switched.

Then the area is:
A = a * b * ( ∫ (Φ dt, π , 0 ) + ∫ ( Φ dt, 2 π , π ) )
(via Hewlett Packard HP 50g and TI nSpire CX CAS)
= a * b * (π^3 / 6 + 7 * π^3 / 6)
= a * b * (4 π / 3)

Slope of a Stretched Spiral

The derivation is similar to the slope of a regular spiral.

∂x / ∂t = a * ( cos t - t * sin t )
∂y / ∂t = b * ( sin t + t * cos t )


Until next time, Eddie.



This blog is property of Edward Shore. (c) 2012

Thursday, March 1, 2012

A Diary of Plots

These are some graphs I posted on both my Twitter and Facebook accounts recently. Enjoy!

Range for Bézier curves: 0 ≤ t ≤ 1

Eddie

Sunday, February 26, 2012

Bézier Curves - Programs for TI nSpire, HP 50g

Introduction

A short definition: a Bézier Curve is a set of parametric equations involving n points:

x(t)=Σ( nCr(n,k) * (1-t)^(n-k) * t^k * x_k) from k=0 to n

y(t)=Σ( nCr(n,k) * (1-t)^(n-k) * t^k * y_k) from k=0 to n

For 0 ≤ t ≤ 1

The curve fits points (x_0, y_0) (when t=0) and (x_n, y_n) (when t=1). The curve approximates the other points.

The degree of the polynomial is n-1.

Bézier curves are used heavily in computer graphics, animation, and font making.

Mike "Pomax" Kamermans provides an awesome primer on Bézier curves.


Béizer Curve Generator - TI nSpire CX
(I am sure this would work with the TI-89 too)

Define LibPub bezier(list)=
Func
© coordinates; Define x (or y)#(t)=bezier(...)
Local n,k
dim(list) - 1 → n
Return Σ( nCr(n,k) * (1-t)^(n-k) * t^k * list[k+1]) from k=0 to n
EndFunc


Save the file, the file name can only be one word. The file must belong to the MyLib folder under the My Documents folder. Please do not forget to refresh the libraries after saving. To refresh, press the doc button, select option 6.

One way to graph a Bézier Curve:

1. Go to, or create a graph screen, or in Scratchpad Graph screen
2. Set to Parametric Mode:

x#(t)=filename\bezier({x coordinates})

y#(t)=filename\bezier({y coordinates})

Set 0 ≤ t ≤ 1

Where # is 1 to 99 and filename is the file that contains the Bezier function.

Bézier Curve for the HP 50g (should work on the 48G and 49G families)

There are two programs in this section. Both are programmed using User RPL.

BEZIER: a curve generator
BEZGRPH: graphs a single Bézier Curve given a list of y coordinates and a list of corresponding x coordinates. BEZIER is required.

Bézier Curve Generator

Level 1: list of coordinates

Program Name: BEZIER
'L' STO
0 'F' STO
'T' PURGE
0 N FOR K
N K COMB
1 'T' - N K - ^ *
'T' K ^ *
L K 1 + GET *
'F' STO+
NEXT F SIMPLIFY FDISTRIB
{N L} PURGE >>


Graphing a Single Bézier Curve:

Level 2: list of y coordinates
Level 1: list of x coordinates

Program Name: BEZGRPH
BEZIER SWAP BEZIER
+ STEQ
PARAMETRIC
{T 0 1} INDEP
AUTO ERASE
DRAW LABEL DRAX PICTURE >>


Notes:
1. The program BEZIER is required.
2. Standard mode (STD) set is used to make labels less obtrusive
3. {T 0 1} INDEP sets T to be the independent variable with the range (0, 1)
Parametric Equations on the HP 50g: XY#(T)=X(T)+i*Y(T)

Example:
Draw a Bézier curve about the points (0,3), (1,2), (0,0), (-1,-1), and (4,-4)
List of y coordinates: {3, 2, 0, -1, -4}
List of x coordinates: {0, 1, 0, -1, 4}

Equations:
x(t) = 4t^4 + 8t^3 - 12t^2 + 4t
y(t) = -5t^4 + 8t^3 - 6t^2 - 4t + 3

TI nSpire CAS CX:

HP 50g:

Hope you enjoyed this blog. Take care as always, Eddie





This tutorial is property of Edward Shore. © 2012

Wednesday, February 22, 2012

An Approach to Plotting Functions in the Complex Plane using Parametric Mode

Graphing functions on the complex plane is not easily handled on a graphing calculator. Few options exist: (1) build a program to build an x-list and a y-list and build a Scatterplot, and (2) use the parametric graphing mode using a numerous amount of paths.

All screen shots are taken with an iPad 2 and a TI nSpire CX CAS calculator.

The complex plane is shown below:

There are pros and cons to each method. The pro of method (1) is that you would get a complete picture of any desired mesh. However, you are also faced with high execution time, even on the best graphing calculators, a lot of RAM usage, and you can usually only use a small area to graph on: for example the interval [-2,2] with 25 point intervals.

Method (2) involves parametric graphing that splits the function into two parts: the real component x(t), and its imaginary component y(t). For any complex function f(z):

x(t) = real(f(z))
y(t) = imag(f(z))

You will either have to separate the real and imaginary components, or if you graphing calculator can handle complex operations, use the real and imag functions.

Caution: If you calcualtor can handle complex numbers, some may not have a built in routine for the trigonometric or advanced operations. This applies to the TI-84 family, and most (if not all) Casio graphing calculators. I list some advanced operations for convenience.

Source: HP-41C Math Pac, Hewlett Packard, 1980?


Let z = x + yi, r = abs(z), θ = arg(z) = angle(z), i = √ -1

ln z = ln r + θ i
a ^ z = e ^ (z ln a)
sin z = sin x cosh y + i cos x sinh y
cos z = cos x cosh y - i sin x sinh y
z ^ n = r ^ n * e ^ ( i n θ )
z ^ ( 1/n ) = r ^ ( 1/n ) * e ^ ( i ( θ / n + ( 2 π k ) / n ) ) , k = 0, 1,...,n - 1
e ^ z = e ^ x * cos y + i * e ^ x * sin y


There is one big obstacle with using the parametric mode. There is only one independent variable, t. So using a set of parametric equations describe only one path.

For example, in graphing sin z set the pair of parametric equations as:
x(t) = real(sin(t + t i))
y(t) = imag(sin(t + t i))

graphs sin z along the path y = x. (x=t, y=t)

In order to get a more complete picture (or really, just more detail), you will have to repeat the pair, but using different paths.


Examples:
For path y = x, use t + t i (sub x = t, y = t)
For path y = -x, use t - t i (sub x = t, y = -t)
For path y = 2x, use 2t + t i (sub x = t, y = 2t)
For path y = e^x, use e^t + t i (sub x = t, y = e^t)
For path y = ln x, use t + e^t * i (sub x = e^t, y = t)
For path y = x^n, use t + t^n * i (sub x = t, y = t^n)
For path y^n = x, use t^n + t * i (sub x = t^n, y = t)


This method is good if you don't want to program, just want to get a quicker picture (method (2) still takes a lot of effort), or you are working with a monochromatic screen (most graphing calculators) where too much detail can just "paint the screen black".

Below are examples of graphs of complex functions from the use of method (2). Keep in mind that these are not complete graphs but enough to get some idea of what is going on.

Hope you enjoyed this blog. Eddie



This blog is property of Edward Shore. © 2012

Wednesday, February 8, 2012

Repeated Applications of Functions



Repeated Functions

If you have ever owned a calculator, chances are you done this: you enter a number and press the square root button many times in a row to see what happens.

For example, you enter say 200. And then you push the square root button. Here are the results for 10 applications of the square root function (to eight decimal places):

14.14213562
3.76060309
1.93922745
1.39256147
1.18006842
1.08630954
1.04226174
1.02091221
1.01040200
1.00518755

Eventually with any positive number, if you press the square root function enough times, you get 1 as a repeated answer.

The following graph shows three functions:

The function in red is the normal square root function.

The function in green shows what happens when the square root function is applied three times to each number.

The function in blue shows what happens when the square root function is applied five times to each number.

Each of the graphs on this blog have been produced using an TI nSpire CAS calculator. All photographs were taken using an iPad 2.

Finding a Closed Form for Repeating the Square Root

If we want to describe what a function would look like when applying the square root n times, we can use a nice, closed, and compact formula.

Let f(x) = √ x = x ^(1/2)

Then

f^n(x) = x ^ 1/2 ^ 1/2 ^ .... ^ 1/2 (n times)
= x ^ (1/ (2^n))
= x ^ (2 ^ -n)

Repeated Sine and Logarithm

Now let's apply common scientific functions and repeat them. First the sine function for x ∈ [-3 π, 3 π].

The graph in blue represents the normal sine function. (x is in radians)

The graph in green is sine repeated 25 times.

The graph in magenta is sine repeated 100 times.

Note with repeated applications of sine, the graph gets more flatter, towards 0, for each point. Could the approximation formula sin x ≈ x have something to do with this?


This next graph shows what happens when you repeatedly apply the natural logarithmic function:


The graph in red is the normal natural logarithmic function.

The graph in green represents the natural logarithm function applied three times.

Something interesting happens with repeating the natural logarithm function. Eventually, you will get a complex number (or an error message on a standard scientific calculator).

Cases in point:

When x=2,
ln 2 = 0.69314718 (to eight decimal places)
ln ln 2 = -0.36651292
ln ln ln 2 = -1.00372150 + π i

When x = 50
ln 50 = 3.91202301 (to eight decimal places)
Repeated applications of the natural logarithm yield:
1.36405463
0.31046161
-1.16969502
0.15674305 + π i

I hope you enjoy this blog, until next time,

Eddie




This blog is property of Edward Shore. © 2012


Monday, February 6, 2012

What a find: An Atari Calculator

I went to the Pasadena City College Swap Meet yesterday, and I found this calculator.

Pictured is the Atari CC 1900 calculator. While it isn't programmable, the calculator features a memory of 32 steps. I picked this up for $5. It looks like this calculator was produced in the 1980s, but I could be wrong on that. Thankfully it runs on two AAA batteries.

This is not the only model of Atari calculators as smaller and solar versions were produced.

Some info about the Atari CC 1900 that I found.

Pasadena City College Flea Market - 1st Sunday of every month

I am looking for a manual.

Take care, Eddie

What to do next?

I think I want to tackle chaos theory or the study of patterns next.

Monday, January 2, 2012

Setting up Equations for Integration/Solve - HP 15C

Setting up equations for the integration and solve functions for the HP 15C. Since the release of the HP 15C Limited Edition, the processing speed has increased.

The most important thing to remember is that the equation starts with "x" on the x-register of the stack.

With integration, "x" is the variable to be integrated.

With the solve function, "x" is the variable to be solved for.

Depending on the equation, in general, you will need to duplicate "x" with [ENTER] as many times as "x" appears in the equation. Algebraic manipulation of the equation can be helpful. A technique known as Horner's Method can be used for polynomials. It also helps to handle the innermost expressions first, working outside.

I often straw a stack diagram:

ST X, ST Y, ST Z, ST T

Several things to remember:

Most two-argument functions (arithmetic, power, combination, permutation, etc):

ST T retains what was in ST T
ST Z copies the contents of ST T
ST Y the contents of ST Z moves here
ST X result of the function

Pressing ENTER, recalling from a memory register, or entering π

ST T the contents of ST Z moves here
ST Z the contents of ST Y moves here
ST Y the contents of ST X moves here
ST X the number just entered or recalled


Horner's Method

Let the polynomial p(x) = a_n * x^n + a_n-1 * x^(n-1) + ... + a1 * x + a0

Applying Horner's Method to p(x):

( ... (a_n * x + a_n-1) * x + a_n-2) * x + a_n-3) ... + a1 ) * x + a0

Functions

Integration: [ f ] [ x ] label

Solve: [ f ] [ ÷ ] label


This blog provides examples of integration, but ideas can be taken from the examples for use in solving equations.


Examples are in the format of:

b
∫ f(x) dx
a

All results shown here are rounded to 4 decimal places (FIX 4).

Example 1:

5
∫ x^2 * cos x dx
1




KEY ST X ST Y ST Z ST T
LBL 1 x - - -
ENTER x x - -
COS cos(x) x - -
x<>y x cos(x) - -
x^2 x^2 cos(x) - -
× f(x) - - -


Result: -19.4578


Example 2:

π
∫ x sin((π * x)/4) dx
0



KEY ST X ST Y ST Z ST T
LBL 2 x - - -
ENTER x x - -
π π x x -
× π*x x - -
4 4 π*x x -
÷ (π*x)/4 x - -
SIN sin(π*x/4) x - -
× f(x) - - -
RTN


Result: 4.1369


Example 3

3.5
∫ x / (x^2 + 3x - 4 ) dx =
3

3.5
∫ x / ((x + 3) *x - 4) dx
3



KEY ST X ST Y ST Z ST T
LBL 5 x - - -
ENTER x x - -
ENTER x x x -
3 3 x x x
+ x+3 x x x
× x(x+3) x x x
4 4 x(x+3) x x
- x(x+3)-4 x x x
1/x 1/... x x x
× f(x) x x x
RTN


Result: 0.0998


Example 4:

3
∫ √(x^3 - 2x + 1)/x dx
1



Let ø = √(x^3 -2x+1)


KEY ST X ST Y ST Z ST T
LBL 3 x - - -
ENTER x x - -
ENTER x x x -
3 3 x x x
y^x x^3 x x x
x<>y x x^3 x x
2 2 x x^3 x
× 2x x^3 x x
- x^3-2x x x x
1 1 x^3-2x x x
+ x^3-2x+1 x x x
√ ø x x x
x<>y x ø x x
÷ f(x) x x x
RTN


Result: 2.0912

Here are a few more examples of integrals. Try and draw the stack diagram for each step.

Example 5:

8.5
∫ x * √(x^2 - 3*x - 4) dx =
4.5

8.5
∫ x * √((x - 3) * x - 4) dx
4.5



LBL 6
ENTER
ENTER
3
-
×
4
-

×
RTN


Result: 117.2455

Example 6:
π/4
∫ x * (( sin(x-2) )/(cos x)) dx
0



LBL 7
ENTER
ENTER
2
-
SIN
x<>y
COS
÷
×
RTN


Result: -0.3578


I hope you find this blog helpful. Until next time, Eddie



This blog is property of Edward Shore. © 2012

3rd Order Runge-Kutta - HP 15C

This program uses a 3rd Order Runge-Kutta method to assist in solving a first order-differential equation.

Given the initial condition (x0, y0) to the differential equation:

dy/dx = f(x, y)

Find the value of y1 where h is a given step size (preferably small). The value of y1 is given by the following equations:

y1 = y0 + k1/4 + (3 * k3)/4

Where:
k1 = h * f(x0, y0)
k2 = h * f(x0 + h/3, y0 + k1/3)
k3 = h * f(x0 + (2 * h)/3, y0 + (2 * k2)/3)

Error estimated on the order of h^4

Source: Smith, Jon M. Scientific Analysis on the Pocket Calculator. John Wiley & Sons, Inc.: New York 1975 pg 174

Memory Registers Used

R0 = h
R1 = x
R2 = y
R3 = x1 (result)
R4 = y1 (result)
R5 = x0 (copied from R1)
R6 = y0 (copied from R2)
R7 = k1
R8 = k2
R9 = k3

Labels Used

Label A = Main Program
Label 0 = Program where equation is maintained

Instructions

1. Store h, x0, and y0 in memory registers R0, R1, and R2 respectively.
2. Enter or edit the equation in program mode using Label 0. Use RCL 1 for x and RCL 2 for y.
3. Run Program A. The first answer is x1. Press [R/S] for y1.
4. To find (x2, y2), store x1 in R1 and y1 in R2 and run Program A again. You can do this by pressing [STO] [ 2 ] [x<>y] [STO] [ 1 ] [ f ] [ √ ] (A) immediately following program execution.


There are 59 program steps in the main program.


Key Code
LBL A 42 21 11
RCL 1 45 1
STO 5 44 5
RCL+ 0 45 40 0
STO 3 44 3
RCL 2 45 2
STO 6 44 6
GSB 0 32 0
RCLx 0 45 20 0
STO 7 44 7
RCL 5 45 5
RCL 0 45 0
3 3
÷ 10
+ 40
STO 1 44 1
RCL 6 45 6
RCL 7 45 7
3 3
÷ 10
+ 40
STO 2 44 2
GSB 0 32 0
RCLx 0 45 44 0
STO 8 44 8
RCL 5 45 5
RCL 0 45 0
2 2
x 20
3 3
÷ 10
+ 40
STO 1 44 1
RCL 6 45 6
RCL 8 45 8
2 2
x 20
3 3
÷ 10
+ 40
STO 2 44 2
GSB 0 32 0
RCLx 0 45 20 0
STO 9 44 9
RCL 3 45 3
R/S 31
RCL 6 45 6
RCL 7 45 7
4 4
÷ 10
+ 40
RCL 9 45 9
3 3
x 20
4 4
÷ 10
+ 40
STO 4 44 4
RTN 43 32



Example 1

dy/dx = x^2 + sin(x * y)

Initial Conditions: (1, 1), let h = 0.01

Program for equation:

 
LBL 0
RAD
RCL 1
x^2
RCL 1
RCLx 2
SIN
+
RTN

Results:

x y
1.0000 1.0000
1.0100 1.0186
1.0200 1.0375
1.0300 1.0568


Example 2

dy/dx = x^3 * y - y

Initial Condition: (1,1); Step size h = 0.01

Program for the equation:
 
LBL 0
RCL 1
3
y^x
RCLx 2
RCL- 2
RTN

Results:

x y
1.0000 1.0000
1.0100 1.0002
1.0200 1.0006
1.0300 1.0014




This blog is property of Edward Shore. © 2012

Happy New Year!

Well 2012 is here! I hope everyone had a safe yet fun and celebratory New Years weekend. This is one of my favorite times of year.

Cheers!

Eddie

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

Numworks (Python): Determining Earth’s Acceleration of Gravity

Numworks (Python): Determining Earth’s Acceleration of Gravity Introduction Note: We will only be using SI units on this blog en...