Saturday, March 16, 2013

HP 39gii Programming Part 3: FOR Loops

This blog entry: I am posting this from the Last Drop Cafè on Claremont, CA - in the college town near the Claremont Colleges. Quite a nice, cozy place. Good coffee too!

-------


Welcome to Part 3!

Table of Contents
1. FOR Loops
2. Other Commands Used in this Lesson
3. The program CROOTS
4. The program POLYSYN
5. The program TIMER



1. FOR Loops

The FOR loop is good construct when you want to repeat a set of commands an amount of times. The commands may (or may not) involve a calculation involving a counter variable. For each pass, the value of counter is incremented (or decremented) until counter matches a target number.

An example is:

FOR K FROM 1 TO 4 DO
A:=A+K;
END;


The counter variable in this example is K. For the first pass, K=1. After 1 is added to A, then the loop ends. K increases by 1. For the second pass, K=2. On so on until the fourth pass where K=4. At that point, K is at its target value and the loop ends.

The increment can be any value you want. If you want the increment to be something other than 1, you can specify that by the STEP instruction. For example,

FOR K FROM 1 TO 4 STEP 0.5 DO
A:=A+K;
END;


Here we have specified the step size to be 0.5, which means that with each pass, K increases by 0.5 (from 1 to 1.5 to 2 to 2.5 and so on until K=4).

The FOR loop for the HP 39gii has four general formats.

Basic FOR loop: Increase counter by an increment of 1:

FOR counter FROM start_value TO end_value DO
insert commands here
END;


Keystrokes for this template (in the program editor): [ F6 ] (TMPLT) [ 4 ] [ 1 ]

FOR loop: Increase counter by an increment other than 1:

FOR counter FROM start_value TO end_value STEP increment DO
insert commands here
END;


Keystrokes for this template (in the program editor): [ F6 ] (TMPLT) [ 4 ] [ 2 ]

For loops with where the value of counter decreases, use the following two formats. Please note at the time of this posting, DOWNTO is an undocumented feature.

FOR loop: Decrease counter by a decrement of 1:

FOR counter FROM start_value DOWNTO end_value DO
insert commands here
END;


FOR loop: Decrease counter by a decrement other than 1:

FOR counter FROM start_value DOWNTO end_value STEP decrement DO
insert commands here
END;


I prefer just typing out the FOR-FROM-DOWNTO/TO-STEP-END structure manually.



2. Other Commands Used in this Lesson

The global variable HAngle. This is the master angle setting. Assigning the following values will result as:

0: Radians Mode
1: Degrees Mode
2: Current Mode

How to find HAngle, if you don't want to type it: [Vars] [ F1 ] [ 4 ] [ 2 ]

CONCAT This command puts two lists together as one.

CONCAT(left list, right list)

Example: CONCAT({1,2,3},{5,6,8}) returns {1,2,3,5,6,8}.

Keystrokes: [Math] [ F1 ] [ 7 ] [ 1 ]

EDITLIST

Syntax: EDITLIST(L#);

This puts the calculator in list editing mode. The command edits the list L# where # is a digit 0-9. You can use a localized list name if you wish instead of the global variable L#.

WAIT

Syntax: WAIT(n);

If n=0, the calculator waits indefinitely. Press any key to have the calculator continue. This is great for freezing the screen to display certain results.

If n>0, the calculator waits n seconds before executing the next step. Useful in loops and displaying multiple bits of information. n does not have to be an integer.

Keystrokes: [SHIFT] [Math] [ F1 ] [ 5 ] [Vars]

The three programs CROOTS (Complex Roots), POLYSYN (Synthetic Division), and TIMER will demonstrate these commands.


3. The Program CROOTS

The first program to demonstrate the power of the FOR loop is the program CROOTS. This program finds all the complex roots of a given number. CROOTS finds all the Nth roots of Z and returns the answer in a list.

Input: CROOTS(Z,N)

The program CROOTS:

EXPORT CROOTS(Z,N)
BEGIN
LOCAL L3,K;
HAngle:=0;
L3:={};
FOR K FROM 0 TO N-1 DO
L3:=CONCAT(L3, {N NTHROOT (ABS(Z)) * e^( i * ( ARG(Z) + 2 * K * π )/N)});
END;
RETURN L3;
END;


Examples (answers are rounded to 5 decimal places)

CROOTS(4,2) returns [2, -2 - 2.5335E-12 * i]. In essence, [2, -2]

CROOTS(-1, 3) returns [0.5 + 0.86603*i, -1*, 0.5 - 0.86603*i].

* -1 - 1.26676E-12*i


4. The Program POLYSYN

Description of POLYSYN:

POLYSYN is a program that divides the polynomial

p(x) / (x-R)

by the use of synthetic division. POLYSYN has no pass-through arguments. The program prompts L1, which is a list of coefficients of descending power to the constants. Please include zeroes whenever necessary. The value of R is also prompted.

The result is a list of coefficients, with the last term the remainder



EXPORT POLYSYN()
BEGIN
LOCAL K,S,T;
EDITLIST(L1);
INPUT(R,"P(X)/(X-R)");
L2:=L1;
S:=SIZE(L1);
T:=S-1;
FOR K FROM 1 TO T DO
L2(K+1):=R * L2(K) + L1(K+1);
END;
MSGBOX("LAST TERM=REMAINDER");
RETURN L2;
END;


L1, R, and L2 are permanently updated.

Example 1: (21x^2 + 42x + 144)/(x - 12) = 21x + 294 + 3672/(x-12)

Inputs:
L1 = {21, 42, 144}
R = 12

Output:
L2 = {21, 294, 3672}

Example 2: (x^3 - 5x + 1)/(x + 5) = x^2 - 5x + 20 - 99/(x + 5)

Input:
L1 = {1, 0, -5, 1}
R = -5

Output:
L2 = {1, -5, 20, -99}


5. The program TIMER

TIMER demonstrates a FOR loop with DOWNTO. The program takes one pass-through argument: the number of seconds.

The program TIMER:

EXPORT TIMER(N)
BEGIN
LOCAL K;
FOR K FROM N DOWNTO 1 DO
PRINT(K);
WAIT(1);
END;
END;



In Part 4 of the HP 39gii Series, we will deal with REPEAT And UNTIL.

As always, thank you - I am grateful for your comments and following this blog. Have a great day!

Eddie


This blog is property of Edward Shore. 2013

Thursday, March 14, 2013

Happy π Day! And some Mathematical Art

Happy π Day!

π = 3.14159 26535 89793 23846 26433 83279 50288...

Who would have known that the ratio of a circle's circumference to its diameter would generate interest though out human history? π never ends, and over 10 trillion digits have been calculate. I have memorized about 15 digits. The digit zero (0) does not appear until the 32nd digit.

Why is π such a "celebrity" number? Find out here:
http://www.youtube.com/watch?v=yJ-HwrOpIps

According to Dr. James Grime, we can calculate the size of the observable universe using π with only 39 digits.
http://www.youtube.com/watch?v=FpyrF_Ci2TQ

You can use the (very slow converging series) to get π:

π = 4 × (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17...)

There are many songs about π. My favorite is "Pi" by Kate Bush (off the album "Aerial").

Tonight I plan to join desmos.com to participate in a π Day discussion.

To celebrate my birthday and π Day, I am going to share some mathematical artwork, which was created using the TI nSpire CX CAS iPad App.

Saturday, March 9, 2013

HP 39gii Programming Part 2: IF/THEN/ELSE/END, MSGBOX, CHOOSE



Table of Contents - Part 2
1. Comparative Operators
2. Storing and assignments
3. IF-THEN-ELSE-END
4. MSGBOX
5. CHOOSE


1. Comparative Operators

The six comparative operators are:

== Equals (Two equal signs)
≠ Not Equals
> Greater Than
≥ Greater or Equal Than
< Less Than
≤ Less or Equal Than

The result of each comparison is either 1 (the comparison is true) or 0 (the comparison is false).

Keystrokes: [Math] [X,T,θ,N],
[ 1 ] for <
[ 3 ] for ==
[ 4 ] for >
[ 9 ] for ≠
[Vars] for ≤
[Math] for ≥


2. Storing and Assignment

There are two ways to store objects:

One is by the STO> command, which usually lives in [ F1 ].

Example: 11>A

The other is by the construct := (colon then equals sign). Keystrokes [ALPHA] [ . ] [SHIFT] [ . ]

Example: A:=11

I am not sure there is any difference between the two storing methods. In this tutorial I will use the latter. (colon followed by an equals sign)


3. IF-THEN-ELSE-END

The structure of IF-THEN-ELSE-END:
IF comparison
THEN commands if the comparison is true;
ELSE commands if the comparison is false;
END;


The ELSE part is optional.

Let's use the IF-THEN-ELSE-END structure in an example. The program ISEVEN, which uses a pass-through argument, tests whether the given number is odd or even.

I usually type the IF-THEN-ELSE-END structure. In the programming edit mode, you can also insert the template with the following keystrokes: [ F6 ] (TEMPLT) [ 2 ] [ 2 ].

The program ISEVEN
EXPORT ISEVEN(N)
BEGIN
N:=INT(N);
IF FRAC(N/2)==0
THEN PRINT(string(N) + " is even.");
ELSE PRINT(string(N) + " is odd.);
END;


Testing:

ISEVEN(25) prints "25 is odd."
ISEVEN(66) prints "66 is even."


4. MSGBOX

MSGBOX is a nice way to display messages in pop-up dialog boxes.

Syntax:
MSGBOX(string of text);

Either type the command or use the following keystroke sequence: [SHIFT] [Math] [ F1 ] [ 5 ] [ 8 ]

Let's demonstrate the use of MSGBOX in the program KEPLER3. KEPLER3 demonstrates Kepler's Third Law. This program in particular tells you how many days an object orbits the Sun of our Solar System. Using U.S. units (I will list SI units too, but the program I demonstrate will use U.S. units)

The mass of the sun is approximately 4.38521 x 10^30 lbs. (1.9891 x 10^30 kg)

The Universal Gravitational constant is about 1.069104 x 10^-9 ft^3/(lb s^2).
(6.67428 x 10^-11 m^3/(kg s^2))

There are 86,400 seconds in a day.

KEPLER3 will also demonstrate the use of local variables. I recommend typing the command LOCAL. This program uses a pass-through argument, average radius.

The Program KEPLER3
EXPORT KEPLER3(R)
BEGIN
LOCAL G,M,T;
G:=1.069104E-9;
M:=4.38521E30;
T:=√((4*π^2*R^3)/(G*M))/86400;
MSGBOX("Time in Days:" + string(T));
END;


Access the "E" in numbers (stands for 10^) by pressing [SHIFT] [X,T,θ,N] (EEX).

Test: If the Earth orbits the Sun in a (near) circular motion maintaining a radius of approximately 490,806,662,402 feet, then approximately how many days does it take the Earth to orbit the sun?

KEPLER3(490806662402) returns in a pop-up box, "Time in Days:365.19645656".

Reasonable estimate don't you think?


5. CHOOSE

The syntax of the CHOOSE command is:
CHOOSE(var, "Title string", "Choice1", "Choice2", ...);

The choice number is stored in var.

CHOOSE can be typed or called using the keystroke sequence [SHIFT] [Math] [ F1 ] [ 5 ] [ 1 ].

If the user cancels out of the choose box, then var will have the value of 0. var can either be a global real variable (A-Z, θ) or a local variable.

The program AREAS will offer the use a choice to calculate the area of three objects: a circle, a ring, or an ellipse. The area of the circle is attached to choice 1, the area of a ring is attached to choice 2, and the area of the ellipse is attached to choice 3.



Caution: Do NOT use CASE. Despite the fact that the command exists in the calculator, the programming language does not support this command. I honestly have no idea whether the next update will correct this.

The program AREAS
EXPORT AREAS()
BEGIN
CHOOSE(C,"Shape","Circle","Ring","Ellipse");
IF C==1 THEN
INPUT(R,"Radius");
MSGBOX("Area="+string(π*R²));
END;
IF C==2 THEN
INPUT(A,"Radius-Large");
INPUT(B,"Radius-Small");
R:=π*(B^2-A^2);
MSGBOX("Area="+string(R));
END;
IF C==3 THEN
INPUT(A,"Semi-Axis 1");
INPUT(B,"Semi-Axis 2");
MSGBOX("Area="+string(π*A*B));
END;
END;


Test data:

Run AREAS thrice:

A circle with radius of 7.26 should return an area of 156.585808948.

A ring with the large radius of 2.07 and small radius of 1.83 will net you an area of 2.94053072376.

An ellipse with semi-axis measurements of 2.3 and 2.6 has an area of 18.7867240685.

In Part 3, the FOR loop will be addressed.

Until next time, Eddie


This blog is property of Edward Shore. 2013

HP 39gii Programming Part 1: INPUT, PRINT, string



Welcome to our HP 39gii Programming Boot Camp!

This is a six part tutorial series in order to get you started programming with the Hewlett Packard HP 39gii calculator. The language may be quirky and imperfect, but rest assured you can still accomplish many things with this programming language.

You can download the emulator here: http://www.hp.com/sbso/product/calculators-emulators/graphic-calculator-emulators.html. It works with Windows, it should work with the Mac (please don't quote me on this).


Contents
1. Creating a Program
2. Structure of a Program
3. The Program AREACIR
4. Running Programs
5. Running AREACIR
6. PRINT, INPUT, and string

Let's get started with our first program, AREACIR. If you guessed that this calculated the area of a circle, you are correct. Gold star for you!


1. Creating a Program
1. Press [SHIFT] [ 1 ] (PRGM).
2. Press [ F2 ] (NEW).
3. Enter the name of your new program. Use [ALPHA] for a single letter, [ALPHA] [ALPHA] to force alphabetical lock, and [SHIFT] to type lower case letters.
4. Press either [ENTER] or [ F6 ] (OK). You are off and running!


2. Structure of a Program
The general structure of a program is:

EXPORT NAME(pass-through arguments)
BEGIN
Commands;
END;


A semicolon (;) follows the end of MOST program lines, all storing commands, and mathematical calculations.

Any arguments that are passed through the program name are designated local variables. Local variables are variables that are used in the program and deleted when done. Global variables are variables that can be accessed inside and outside programming. The HP 39gii restricts the types of global variables.

Type of global variables:
Real: A through Z, θ
Matrices: M0 through M9
Lists: L0 through L9
Complex Numbers: Z0 through Z9

Almost everything else, including strings, must be local variables. To declare any variable, including global variable types, type:

LOCAL var, var, var, ...;

Include as many variables as you want. When you select LOCAL from the Cmds menu, it will give you a set parenthesis. Erase them, LOCAL does not work with parenthesis.

Let's get to our first program, AREACIR. AREACIR will have one pass-through argument, the radius.


3. The Program AREACIR
EXPORT AREACIR(R)
BEGIN
π*R²;
END;


This program returns the area of a circle to the home screen.


4. Running Programs

You can run programs from two places: the Home Screen or thought the Program Catalog. I recommend the former (Home Screen) if you have pass-through arguments.

From the Home Screen:

1. Press [Home]
2. Press [SHIFT] [Math] (Cmds)
3. Press [ F3 ] (USER)
4. Find the program you want to run, pressing [ENTER] or [ F6 ], twice.
5. If any pass-through arguments are required, include the parenthesis and arguments. Otherwise, just press [ENTER].

You may also type out the program's with any required arguments.


5. Running AREACIR

Find the area of the circle with radius 17.5 units.

Using the Cmds menu from Home:

1. Press [Home]
2. Press [SHIFT] [Math] (Cmds)
3. Press [ F3 ] (USER)
4. Find the program you want to run, pressing [ENTER] or [ F6 ], twice.
5. Press [ ( ] 17.5 [ ) ] [ENTER]

Alternatively, you can type AREACIR(17.5) [ENTER]

The area is approximately 962.112750162.


6. PRINT, INPUT, and string

Our next program will do the same as AREACIR, except instead of using pass-through arguments, we will use prompting and displaying answers on an output terminal.

INPUT

Keystrokes: [SHIFT] [Math] [ F1 ] [ 5 ] [ 6 ]
Command can be typed
Basic Syntax: INPUT(global variable, "title string");

INPUT can have more arguments, check the HP 39gii manual. INPUT only works with one variable at a time and works only variables that can be global. INPUT does not work when promoting for strings.

In practice, I recommend that you use pass-through arguments instead of INPUT.

PRINT

Keystrokes: [SHIFT] [Math] [ F1 ] [ 5 ] [ 9 ]
Command can be typed
Basic Syntax: PRINT(string of text);

Note that strings of text are connected with the + operator.

string

This commands evaluates a mathematical expression and makes the result a string. This is useful for displaying text with mathematical calculations.

Keystrokes: [SHIFT] [Math] [ F1 ] [ 8 ] [Vars]
Command can be typed, but the command must be typed in lowercase!
Basic Syntax: string(mathematical expression or result);

Let's use INPUT, PRINT, and string in a program:

The Program AREA1:

EXPORT AREA1()
BEGIN
INPUT(R, "Radius");
PRINT("THE AREA IS " + string(π*R²) + ".");
END;


Running AREA1: Type or call AREA1 at the Home Screen. Since there are no pass-through arguments, you won't need parenthesis.

You will be prompted for R. If I enter 17.5 at the prompt, I will get a screen that contains the message:

THE AREA IS 962.112750162.

When you press ENTER, that string will be returned to the Home screen.


Part 2: IF-THEN-ELSE, MSGBOX, and CHOOSE.

Have a great day!


Eddie


This blog is property of Edward Shore. 2013

Thursday, March 7, 2013

Update: The HP 39gii Tutorial Will Go On and Pi Day

Thanks to HP - I was able to get 39gii up and running again. I am currently writing Part 2. I aim to get at least Parts 1 and 2 up by next week.

Pi Day (March 14) is one week away! Any suggestions? How do you celebrate Pi Day?

π = 3.1415926535...


Eddie

Tuesday, March 5, 2013

HP 39gii Programming Series Delayed (Maybe Cancelled)

With regrets, I am going to have to delay and/or cancel the HP 39gii calculator programming tutorial series. My 39gii is giving me problems - it has an issue with freezing.

Apologies. I will give an update as soon as I can.

Eddie

This blog is property of Edward Shore. 2013

Saturday, February 23, 2013

Greetings From Riverside! Mike Grigsby's Blog and an TI nSpire App Review

I am blogging today from Daily Brew Coffee House in Riverside, CA. Service here is excellent - very nice and friendly people.



Mike Grigsby

If you get a chance, please check out Mike Grigsby's blog at http://mikebibbygrigsby83.blogspot.com/?m=1. Mike has a Master's Degree in Mathematics from Cal Poly Pomona, and is the Social Media Administrator for the Riverside Astronomical Society. Later tonight I am going to meet with Mike to go their monthly meeting at La Sierra University in Riverside.

More information about the Society can be found here: http://www.rivastro.org/index.php.

Mike is a super duper awesome guy. Please check his blog out.


Quick Review: Texas Instruments TI-nSpire App for iPad

Platforms: iPad (iPad 2 and later, including the iPad Mini)
Cost: $29.99 (yes, this is very expensive for an app). There is a CAS and non-CAS version. I totally recommend that you get the CAS version. (CAS is the Computer Algebraic Version)
Version of nSpire: TI nSpire CX (the latest and the good one in my opinion)

Short and sweet, the nSpire app is everything the TI nSpire hardware is. Unlike most calculators, the nSpire operates as a computer software interface (think closer to Mathematica, Matlab, or Maple) than the traditional calculator. You work on documents which contains pages. Each page can be a mathematics page, graph page, a spreadsheet, geometry page, or a statistics page. Within the problems in the document, variables are linked. That is, you can create a variable in one page, and it is define throughout the documents. Programs work the same way.

Programs are in TI-Basic and can either be limited to the document or made global. The global feature is very useful when you are creating custom functions (like the gamma function). Programming on the nSpire is more strictly for the mathematical sense; so if you are looking to make video games, this app may not be what you are looking for.

The keyboard on the app is a pleasure to use. On the keyboard, you will note several keys having a blue bar over them. Holding the key down gives you additional options. For example:
Holding the r key gives you access to the θ variable.
The var key contains the STO> function.
The sin key expands to sin^-1, csc, and csc^-1.

The best part of the keyboard? The keys are big! Well, we are on an iPad here.

On the graph page, not only can you graph functions, polar functions, scatter plots, parametric functions, and inequalities, you can do it all one page if you want. This adds flexibility that is usually seen only on the Casio graphing calculators and somewhat on the HP 49g series. The graph page offers a geometric view or a 3D view for three-dimensional graphs. Pinch to zoom, hold on to drag the graph and any other text.

Here are the screenshots from the app. All the shots are from the CAS version.

I recommend getting this app. Now I realize that the $29.99 price tag is heavy in the market on apps, but this app has it all together in one package. However, in the market of complete mathematical software packages it is inexpensive. If you buy it, I recommend the CAS version.

This is also a great alternative to getting the hardware if you don't want and/or have $150 to spend but have an iPad ready. And the batteries last longer! At least with the app I don't have to constantly plug in my tangible nSpire CX almost every time I want to use it. And yes documents can be shared by email or downloaded to the computer for later use (including downloading it on the physical units).




That is all for today, please go check out Mike Grigsby's blog. I can't wait to meet with him tonight. Have a great day and weekend everyone.

Eddie


This blog is property of Edward Shore. 2013

Wednesday, February 20, 2013

TI-84+: Binary-Decimal Conversions

One of the missing features of the TI-82/83/84 family is the ability to convert between bases. Here are two programs in TI-Basic to help fill at least some of the gap.

It is very basic conversion, working only with positive integers up to 65,535 (16 ones as its binary representation).

Variables used:
N = number in decimal form
L1 = list representing the binary representation (1s and 0s)

The programs display the binary numbers as a solid number, rather by a list. This is accomplished by a For loop involving the Output command.

Access L1 by pressing [2nd], [ 1 ].

Examples: Decimal ← → Binary
27 ← → {0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1}
428 ← → {0,0,0,0,0,0,0,1,1,0,1,0,1,1,0,0}
3,245 ← → {0,0,0,0,1,1,0,0,1,0,1,0,1,1,0,1}

DEC2BIN
Decimal to Binary (N → L1)
This program works with any positive integer from 0 to 65,535 - 16 bits. No negative numbers. Note: ending quotes and parenthesis are left out to conserve space.
2/20/2013. 170 bytes.  (updated 7/5/2016)

: Input "N:",N
: If N<0 

: Then
: Pause "INVALID
: Stop
: End
: int(N→N
: N→D
: DelVar L1
: 16→dim(L1
: For(K,0,15
: If 2^(15-K)≤D
: Then
: 1→L1(K+1
: D-2^(15-K→D
: End
: End
: ClrHome
: Output(1,1,N
: Output(1,6,">BIN
: For(K,1,16
: Output(3,K,L1(K
: End
: Pause
: ClrHome


BIN2DEC
Binary to Decimal (L1 → N). Enter a list up to 16 zeroes and ones.
This program works with any positive integer from 0 to 65,535 - 16 bits. No negative numbers. Note: ending quotes and parenthesis are left out to conserve space.
2/20/2013. 160 bytes.


: Input "L1 UP TO 16 BITS:",L1
: If dim(L1)>16
: Then
: Pause "INVALID
: Stop
: End
: While dim(L1) < 16

: augment({0},L1→L1
: End
: 0→N
: For(K,0,15
: N+2^K*L1(16-K→N
: End
: ClrHome
: For(K,1,16
: Output(1,K,L1(K
: End
: Output(3,1,">DEC
: Output(3,6,N
: Pause
: ClrHome


** Edited 12/5/2013.  This is due to an error Stephanie Ison pointed out to me.  Many thanks! - Eddie 

Enjoy!

Eddie


This blog is property of Edward Shore. 2013




Saturday, February 16, 2013

Review: Programming the TI-83 Plus/TI-84 Plus by Christopher R. Mitchell

Continuing the Saturday night tour, I stopped by at another coffee place, the Swörk Urban Coffee Bar in Eagle Rock (Los Angeles).

Review: Programming the TI-83 Plus/TI-84 Plus - Christopher R. Mitchell

Author: Christopher R. Mitchell, his website is http://www.cemetech.net/.

To make this short and sweet, this book is an excellent reference book, for all levels. I have 22 years of calculator programming experience - starting from the TI-81 in 1991. However I am learning new tricks and techniques from this book.

The book aims towards the TI-83/TI-84 family and how to maximize programming efficiency. I focused on the later chapters of the book, where topics cover the GETKEY command, using the home and graph screen to efficiently display text and figures, and how to make programs interactive. There is also a chapter on how to optimize programs, such as taking advantage of implicit conditionals and use of the last answer (Ans) function. So far, the GETKEY is my favorite thing I am learning.

The list price is $29.99, which includes the book and electronic copies of the book (I have yet to download the electronic copy.) If you want to expand your programming skills, or develop them from the beginning - Go for it, I definitely recommend this book. Mitchell writes the book in a straight-forward manner.

P.S. I got to the mouse to eat 142 pieces of cheese. (See chapter 6 to see what I am taking about, readers).

Eddie




This blog is property of Edward Shore. 2013



a_(n+1) = a_n + n + C (from Stories Cafè in Echo Park in Los Angeles)

Good afternoon all! This is a Saturday Afternoon/Night blog post. On the menu: much needed delicious food from Stories Cafè in Los Angeles and some work with recurrence relations.

But first, an announcement! I am working on a short series of programming tutorials of the Hewlett Packard HP 39gii Calculator. Think of it as a short boot camp to get HP 39gii owners up and running.

An emulator for the HP 39gii can be found here: http://www.hp.com/sbso/product/calculators-emulators/graphic-calculator-emulators.html. The emulators are for Widows and probably Mac - not sure if they run on other operating systems. As of today, I am not aware if there are any iOS and/or Android apps for this. I aim to post this series in March 2013.

Now on to the math...

a_(n+1) = a_n + n + C

The goal is to find a general formula for

(I) a_(n+1) = a_n + n + C with the initial condition a_0 = A_0.

The type of recursion formula presented is polynomial recurring relation. A general polynomial recursion formula takes the form a_(n+1) = a_n + p(n).

The general formula for a_(n+1) = a_n + n + C takes the form:

(II) a_n = c_2 * n^2 + c_1 * n + c_0

Mainly, with recursion formula of polynomial type will require a formula of order n+1. There are n+2 constants to solve for.

Working with equation (II) above, we will need to solve for c_0, c_1, and c_2. It would be very difficult to work with only one equation. Luckily, we can turn this problem in to a system of three linear equations.

Starting with the initial condition a_0 = A_1, we can use the recursion formula for n=1 and n=2 to find a_1 and a_2. Then we get the system:

(III)
a_0 = c_0 (where n=0)
a_1 = c_0 + c_1 + c_2 (where n=1)
a_2 = c_0 + 2 * c_1 + 4 * c_2 (where n=2)

Putting (III) in matrix form we get:

(IV)
[ [1, 0, 0], [1, 1, 1], [1, 2, 4] ] * [ [c_0], [c_1], [c_2] ] = [ [a_0], [a_1], [a_2] ]

For the Hewlett Packard HP 50g calculator owners (others may have this function/program), you can get the matrix on the left hand side of equation (IV) by using the VANDERMONDE matrix function on the vector [0, 1, 2]. This function comes in handy for these types of problems.

Continuing with the ever handy HP 50g, solving for c_0, c_1, and c_2:

(V)
[ [c_0],[c_1],[c_2] ] = [ [1, 0, 0],[-3/2, 1, -1/2],[1/2, -1, 1/2] ] * [ [a_0], [a_1], [a_2] ]

And we finish with:
(VI)
c_0 = a_0
c_1 = -3/2 * a_0 + 2 * a_1 - 1/2 * a_2
c_2 = 1/2 * a_0 - a_1 + 1/2 * a_2


So in summary:
The general formula for the recursion formula

a_(n+1) = a_n + n + C is:

c_0 = a_0
c_1 = -3/2 * a_0 + 2 * a_1 - 1/2 * a_2
c_2 = 1/2 * a_0 - a_1 + 1/2 * a_2



A numerical example:

a_(n+1) = a_n + n - 2 with a_0=3

Prepare by finding a_0, a_1, and a_2:

a_0 = 3 (given)
a_1 = 3 + 1 - 2 = 2 (use n=1)
a_2 = 2 + 2 - 2 = 2 (use n=2)

Then:
c_0 = 3
c_1 = -3/2 * 2 + 2 * 2 - 1/2 * 2 = -3/2
c_2 = 1/2 * 2 - 2 + 1/2 * 2 = 1/2

Our general formula is:
a_n = 1/2 * n^2 - 3/2 * n + 3

Let's test it out.

With n=1:
a_1 = 1/2 - 3/2 + 3 = 2 (checks out)

With n=3
a_3 = 1/2 * 3^2 - 3/2 * 3 + 3 = 3

Observe from the recursion formula a_3 = 2 + 3 - 2 = 3. (Checks out)

With n=4
a_4 = 1/2 * 4^2 - 3/2 * 4 + 3 = 5


Warning: The above technique only works when the coefficient of a_n is 1. (See (I).)

I tried this technique with a_(n+1) = -3*a_n + 7*n - 1 with a_0 =1.

With a_1 = 5 and a_2 = 0 from the recursion formula, I came up with a_n = -9/2 * n^2 + 17/2 * n + 1. Using this formula I came up with a_3 = -14, when in reality a_3 = 22. Just a heads up.


I hope this weekend and all your future days are good to you.

Eddie


This blog is property of Edward Shore. 2013

Saturday, February 9, 2013

a_(n+1) = S * a_n + T

Hi everyone!


First a correction. In my Numerical CAS section, posted December 2012, I typed an extra quotation mark in the POLYBINE program for the Casio Prizm - that has been corrected. Much thanks to Ryan Maziarz who pointed that out to me.

Here is a link to the corrected post:
http://edspi31415.blogspot.com/2012/12/numeric-cas-part-2-binomial-expansion.html?m=1


Today we are finding the general formula for this recursion formula:

a_(n+1) = S * a_n + T

with the initial condition a_0 = A.

I could try using a characteristic polynomial, but with some observation we may be able to detected a pattern which leads us to a general formula.

Observe that:

a_0 = A

a_1 = S * a_0 + T
a_1 = S * A + T

a_2 = S * a_1 + T
a_2 = S * (S * A + T) + T
a_2 = S^2 * A + S * T + T
a_2 = S^2 * A + T * (S + 1)

a_3 = S * a_2 + T
a_3 = S * (S^2 * A + T * (S + 1)) + T
a_3 = S * A^3 + T * (S^2 + S + 1)

a_4 = S * a_3 + T
a_4 = S^4 * A + T * (S^3 + S^2 + S + 1)

From the pattern, observe that:

a_n = S^n * A + T * Σ(S^k, k=0, n-1)

With Σ(t^k, k=0, n-1) = (t^n - 1)/(t - 1) :

a_n = S^n * A + T * (S^n - S)/(S - 1)


An Example:

S = 3, T = 1, A = -2

Using the HP 39gii to generate the sequence (the 39gii uses a_1 as its initial term):


The general formula is:

a_n = (3^n) * (-2) + 1 * (3^n - 1)/(3 - 1)
a_n = (-2) * 3^n + (3^n - 1)/2
with a_0 = -2. (The 39gii has u_1 = a_0)

Example:

a_2 = u_3 = (3^2) * (-2) + (9 - 1)/2 = -14


That is all for now, see you next time!

Eddie

This blog is property of Edward Shore. 2013

5 blog entries away from the 200th blog entry.

Greetings From Monrovia! Finding a Formula to Generate the Fibonacci Sequence

Hi everyone. I am blogging from the Friends Café in downtown Monrovia, CA on a beautiful, chilly day.

Today's blog entry is about to the Fibonacci Sequence and how to find a general formula to generate a sequence.

The Fibonacci Sequence

The world famous Fibonacci Sequence, first introduced by Fibonacci's Liber Abaci, written in 1202, is:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 293, 377...

(Source: Pickover, Clifford. "The Math Book" Sterling Publishing, New York. 2009 - Eddie says: Go get this book - it's awesome!)

It is easy to build the sequence. Start with two entries of 1. Add them up to get the next term. Each subsequent term is the sum of the last two. 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13, and so on.

We can use this recursion formula:

a_(n+2) = a_(n+1) + a_n

with the initial conditions a_0 = 1 and a_1 = 1.

We can generate a general formula for this.


Technique

Start with the recursion formula:

a_(n+2) = a_(n+1) + a_n

with the initial conditions a_0 = 1 and a_1 = 1.

This is a linear recursion formula. A way to find the general formula is to use a characteristic polynomial. The characteristic polynomial is formed by turning subscripts to exponents (and usually replacing a with another letter, such as x) and finding the roots of the polynomial.

The general formula will have the form

a_n = c_1 * (x_1)^n + c_2 * (x_2)^n + ...

Where n is the number of initial conditions.

In the case of the Fibonacci sequence, we have two initial conditions (a_0 = 1 and a_1 = 1). Hence n=2.

Then use the initial conditions to solve for the c_k constants.


Finding the General Formula

a_(n+2) = a_(n+1) + a_n
with the initial conditions a_0 = 1 and a_1 = 1.

The characteristic equation is:

x^(n+2) = x^(n+1) + x^n

Assuming x ≠ 0, divide both sides by x^n:

x^2 = x + 1

x^2 - x - 1 = 0

The roots of the polynomial are:

x_1 = (1 + √5)/2
x_2 = (1 - √5)/2

Then our general form has:

a_n = c_1 * ((1 + √5)/2)^n + c_2 * ((1 - √5)/2)^n

Our next step is to find c_1 and c_2.

Note when n = 0, a_0 = 1 and:

1 = c_1 + c_2

When n = 1, a_1 = 1 and:

1 = c_1 * (1 + √5)/2 + c_2 * (1 - √5)/2

Leaving us with the system of linear equations:

c_1 + c_2 = 1
c_1 * (1 + √5)/2 + c_2 * (1 - √5)/2 = 1

I like using matrices to solve systems of linear equations, you may a different preferred way, it's all good here:

[ [1, 1], [(1 + √5)/2, (1 - √5)/2] ] * [ [c_1], [c_2] ] = [ [ 1 ], [ 1 ] ]

[ [c_1], [c_2] ] = [ [1, 1], [(1 + √5)/2, (1 - √5)/2] ]^-1 * [ [ 1 ], [ 1 ] ]

[ [c_1], [c_2] ] = [ [(5 - √5)/10, √5/5], [(5 + √5)/10, -√5/5] ] * [ [ 1 ], [ 1 ] ]

which evaluates and simplifies to:

c_1 = (5 + √5)/10
c_2 = (5 - √5)/10

The general formula to generate the Fibonacci sequence is:

a_n = (5 + √5)/10 * ((1 + √5)/2)^n + (5 - √5)/10 * ((1 - √5)/2)^n


An approximate formula would be (8 digits):

a_n ≈ 0.72360680 * 1.6180339^n + 0.27639320 * (-0.6180339)^n

Round off when necessary.

Testing the general formula, find the 2nd and 6th term:

a_2 = (5 + √5)/10 * ((1 + √5)/2)^2 + (5 - √5)/10 * ((1 - √5)/2)^2
a_2 = (5 + √5)/5 + (5 - √5)/2
a_2 = 10/2 = 2

a_6 = (5 + √5)/10 * ((1 + √5)/2)^6 + (5 - √5)/10 * ((1 - √5)/2)^6
a_6 = (65 + 29 * √5)/10 + (65 - 29 * √5)/10
a_6 = 130/10 = 13


A Broader Problem

The technique applied can be applied to the general problem:

a_(n+2) = S * a_(n+1) + T * a_n

With initial conditions a_0 = A and a_1 = B.

Using the trusty HP 50g to assist me, I come up with the following:

a_n = c_1 * (x_1)^n + c_2 * (x_2)^n

where

x_1 = (S + √(S^2 + 4 * T))/2

x_2 = (S - √(S^2 + 4 * T))/2

c_1 = (B - A * x_2)/(x_1 - x_2)

c_2 = (A * x_1 - B)/(x_1 - x_2)


Next time, I am going to look at the general recursion formula:

a_(n+1) = S * a_n + T with the initial condition a_0 = A

I am working a basic programming series, which my target is with the HP 39gii calculator and the series would be posted in March 2013.

Thank you everyone who reads, follows, and comments on this blog. As always it is much appreciated.

Have a great day!

Eddie


This blog is property of Edward Shore. 2013

Friday, February 1, 2013

Balances and Recursion Formula - Part 2


To recap our scenario:

We have a loan with an initial balance $b, that has an annual interest rate of r%. We are making a monthly payment of $p every month. In Part 1, we were finding out how long to pay off the balance.

In Part 2, we asking the same question, but with an added component: this time assume that there is a monthly charge of $c to the balance. The charge stops when the balance is paid.


Variables:
b = a_0 = initial balance
p = monthly payment
r = annual rate
c = monthly additional charge
a_n = balance after n payments

Let the recursion formula be:

a_(n+1) = (a_n + c) + (a_n + c) × r/1200 - p
with the initial condition a_0 = b.

Let θ = r/1200, then:
a_(n+1) = (a_n + c) × (1 + θ) - p

Let's get a_1, a_2, and a_3 in terms of a_0.

a_1 = (a_0 + c) × (1 + θ) - p

a_2 = (a_1 + c) × (1 + θ) - p
a_2 = ((a_0 + c) × (1 + θ) - p + c) × (1 + θ) - p
a_2 = (a_0 + c) × (1 + θ)^2 - p × (1 + θ) + c × (1 + θ) - p

a_3 = (a_2 + c) × (1 + θ) - p
a_3 = a_2 × (1 + θ) + c × (1 + θ) - p
a_3 = ((a_0 + c) × (1 + θ)^2 - p × (1 + θ) + c × (1 + θ) - p) × (1 + θ) + c × (1 + θ) - p
a_3 = (a_0 + c) × (1 + θ)^3 - p × (1 + θ)^2 + c × (1 + θ)^2 - p × (1 + θ) + c × (1 + θ) - p

Noticing a pattern...

a_n = (a_0 + c) × (1 + θ)^n - p × Σ((1 + θ)^k, k=0, n-1) + c × Σ((1 + θ)^k, k=1, n-1)
a_n = (a_0 + c) × (1 + θ)^n - p × ((1 + θ)^n - 1)/θ + c × ((1 + θ)^n - (1 + θ))/θ

With a_0 = b:

a_n = (b + c) × (1 + θ)^n - p × ((1 + θ)^n - 1)/θ + c × ((1 + θ)^n - (1 + θ))/θ


Example 1:

Sandra has a credit card with a 15% APR. The initial balance is $1,500.00. Sandra uses a premium credit card that charges $20.00 each month there is a balance.

b = 1500.00
c = 20.00
r = 15%
p = 500.00

Then θ = 15/1200 = 1/80

The resulting sequence is:

a_0 = 1500.00
a_1 = 1039.00
a_2 = 572.23
a_3 = 99.64

In 3 payments , Sandra has knocked the balance to below the payment amount of $1,500.00. In month 4, the amount will be ($99.64 + $20.00) × (1 + 15/1200) = $121.14, and her debt will be over.

Example 2:
Greg has taken a payday loan, for the amount of $1,893.64. The interest rate is 14.99% and the loan incurs a $14.99 holding fee for each month the balance exists. Greg makes a $350.00 payment every month.

b = 1893.64
c = 14.99
r = 15.99%
p = 350.00

Then θ = 15.99/1200

The sequence generated is:

a_0 = 1893.64
a_1 = 1584.06
a_2 = 1270.36
a_3 = 952.48
a_4 = 630.36
a_5 = 303.95

At month 6, the final balance will be ($303.95 + $14.99) × (1 + 14.99/1200) = $322.92.


We can find out when the balance is zero by solving for n.

When a_n = 0:

n = ln((c × (1 + θ)/θ - p/θ) / (b + c - p/θ +c/θ)) × (ln (1 + θ))^(-1)

Then the integer part of n, int(n), is the nth payment when the balance is less than the payment amount.

Recalling our examples:

Example 1:

b = 1500.00
c = 20.00
r = 15%
p = 500.00

Then θ = 15/1200 = 1/80, and n ≈ 3.21

This means the balance is below the payment after then 3rd payment.

Example 2:

b = 1893.64
c = 14.99
r = 15.99%
p = 350.00

Then θ = 15.99/1200 and n ≈ 5.91

The balance becomes below the payment after the 5th payment.


Have a great day everyone!

Eddie

This blog is property of Edward Shore. 2013

Balances and Recursion Formula - Part 1

Greetings everyone coming to you from a Starbucks in Covina, CA! (on my way to seeing my mom and some friends for dinner).

1 month of 2013 is already in the books - hope everyone is getting February off to a great start!

The topic of this and the next post: using a recursion formula in financing.



The scenario: You have a loan that you make monthly payments. Interest accrues each month based on the loan's balance. The rate is stated as an annual rate. How many months will it take to pay off the loan?

For this, we are going to use a recursion formula to describe the balance at the end of each payment. We will then come up with a general formula which will allow us to find the balance at any month. Finally, use the general formula to find when the balance is zero (approximately).

Part 1 will deal with the scenario stated. On my next blog entry, I will consider paying the loan balance when a monthly charge is involved.


What is a recursion formula?

A recursion formula is a formula in which the result is based on previous results. To generate a sequence of answers, one or more initial conditions are required.

A simple example is:

a_(n+1) = 2 × a_n + 1 with the initial condition a_0 = 0.

In order to calculate a_(n+1), you will need the previous result, a_n.

Then: (a_n is in bold)

a_0 = 0 (start with the initial condition)
a_1 = 2 × 0 + 1 = 1
a_2 = 2 × 1 + 1 = 3
a_3 = 2 × 3 + 1 = 7
a_4 = 2 × 7 + 1 = 15
And so on...

Some recursion formulas can be transformed into general formulas, where any nth term can be found when only the initial conditions are known.

A famous recursion formula is the Fibonacci Sequence (1, 1, 2, 3, 5, 8, 13, 21, etc...)

a_(n+2) = a_(n+1) + a_n with the initial conditions a_0 = 1 and a_1 =1.


Back to our scenario: You have a loan that you make monthly payments. Interest accrues each month based on the loan's balance. The rate is stated as an annual rate. How many months will it take to pay off the loan?

Variables:
b = a_0 = initial balance
p = monthly payment
r = annual rate (if the rate is 10%, r = 10)
a_n = balance after n payments (after n months)

Let the recursion formula be:
a_(n+1) = a_n + a_n × r/1200 - p
with the initial condition a_0 = b

Let θ = r/1200. Then:

a_(n+1) = a_n × (1 + θ) - p

Note: we will stop when the balance sinks below the payment amount. At that point, the next payment is the balance, reducing the debt to 0.

Calculating the first few terms (balance after n payments) in terms of the initial balance (a_0):

a_1 = a_0 × (1 + θ) - p

a_2 = a_1 × (1 + θ) - p
a_2 = (a_0 × (1 + θ) - p) - p
a_2 = a_0 × (1 + θ)^2 - p × (1 + θ) - p

a_3 = a_2 × (1 + θ) - p
a_3 = (a_0 × (1 + θ)^2 - p × (1 + θ) - p) × (1 + θ) - p
a_3 = a_0 × (1 + θ)^3 - p × (1 + θ)^2 - p × (1 + θ) - p

Noticing the pattern...

a_n = a_0 × (1 + θ)^n - Σ( p × (1 + θ)^k, k=0, n-1)
a_n = a_0 × (1 + θ)^n - p × Σ( (1 + θ)^k, k=0, n-1)
a_n = a_0 × (1 + θ)^n - p × ((1 + θ)^n - 1)/(1 + θ - 1))

With a_0 = b:

a_n = b × (1 + θ)^n - p × ((1 + θ)^n - 1)/θ


Example 1:

A bank loan is made for $1,000, which has a 10% annual interest rate. The borrower plans to make monthly payments of $300.

r = 10%, b = 1000, p = 300
Then θ = 10/1200 = 1/120

The sequence generated is:

a_0 = 1000.00
a_1 = 708.33
a_2 = 414.23
a_3 = 117.68

Example 2:

The balance on a short term lease is currently $8,764.10. The lessor wants to make payments of $1,500 until the balance is fully paid. The lease carries an interest rate of 4.95%.

r = 4.95%, b = 8764.10, p = 1500.00
Then θ = 4.95/1200 = 33/8800

The sequence generated is:

a_0 = 8764.10
a_1 = 7300.25
a_2 = 5830.37
a_3 = 4354.42
a_4 = 2872.38
a_5 = 1384.27


When will the balance be fully paid?

Solving for n when a_n = 0:

0 = b × (1 + θ)^n - p × ((1 + θ)^n - 1)/θ
0 = (b - p/θ) × (1 + θ)^n + p/θ
-p/θ = (b - p/θ) × (1 + θ)^n
-[ (p/θ) / (b - p/θ) ] = (1 + θ)^n
[ (p/θ) / (p/θ - b) ] = (1 + θ)^n
ln [ (p/θ) / (p/θ - b) ] = n × ln (1 + θ)

n = ln [ (p/θ) / (p/θ - b) ] / ln (1 + θ)

Taking the integer portion of n, or int(n), gives the nth payment when the balance is less than the payment amount.


Continuing with our two examples from before:

Example 1:

r = 10%, b = 1000, p = 300
Then θ = 10/1200 = 1/120

n ≈ 3.3

After 3 payments, the balance is below the payment amount of $1,000.00.

Example 2:

r = 4.95%, b = 8764.10, p = 1500.00
Then θ = 4.95/1200 = 33/8800

n ≈ 5.9

After 5 payments, the balance is below the payment amount of $1,500.00.


Next time will be Part 2. Thanks as always!

Eddie


This blog is property of Edward Shore. 2013



P.S. To all those who are partaking in the Super Bowl festivities this Sunday: please be safe - and don't drink and drive! And have fun.

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026

Sharp EL-5200/EL-9000 AER II Program Collection – September 2026 For my review on the Sharp EL-5200 (also known as the Sharp EL-9000)...