Showing posts with label tips and tricks. Show all posts
Showing posts with label tips and tricks. Show all posts

Saturday, October 5, 2019

Fun with the TI-81: Part I

Fun with the TI-81:  Part I

Before there there was the TI-82, TI-83 and its family, and the TI-84 Plus and its family, there was the Texas Instrument's original calculator from the 1990, the TI-81!

Translating TI-81 to TI-84 Plus (and Back) 

Most of the commands can be copied directly.  Some caveats to keep in mind:

Disp

The TI-81's Disp (Display command) can only display either a variable's value or a string.  The command can only display one item per line.

The TI-84 Plus' Disp command can list any combination of strings and variable values, separated by a comma.  Each argument will be placed on one line.

Input

The TI-81's Input command can ask for one variable.  There is no prompt string option.  A prompt string will require an extra Disp command.  Also, there is no colon character.

Disp "VAR="
Input A

The TI-84 Plus' Input command can have an optional prompt string.

Input "VAR=", A

Lists 

The TI-81 has two lists that are used for statistical calculations, {x} and {y}.  To recall an element of either {x} or {y}, press [ 2nd ] [ 0 ] or [ 2nd ] [ 1 ], respectively.  The dimensions of the stat lists can be found by pressing [VARS], and selecting Dim{x} under the DIM menu.  Stat lists can't be resized by storing a value to it.

All of the lists for the TI-84 Plus start with a lower case bold "L".  Lists 1-6 can be pressed by [ 2nd ] [ 1 ] through [ 6 ].  There are many lists commands and functions for the TI-84 Plus.

Linear Regression Options

Running linear regression is the LinReg command on the TI-81.  The equation will always be a + bx.

Running linear regression for the TI-84 Plus will need you to designate the x-list and y-list.  There are also various options: a + bx, ax + b, or Med-Med

The If Command and Loops

The TI-81 only has a singular If command, no Then or Else.  The syntax is:

If condition
do if condition is true
skip to here if condition is false

Loops will require the extensive use of Lbl (label), Goto, DS<(, and IS>(.   Lbl and Goto are self-explanatory. 

DS<(var, target).   The value of var is decreased by 1.  The next command is skipped when value < target.

IS>(var, target).  The value of var is increased by 1.  The next command is skipped when value > target. 

In addition to If (which can still do the two-line structure), Lbl, Goto, DS<(, and IS>(, the TI-84 Plus has Then, Else, For, While, and Repeat.

The STO> Button

The TI-81 turns on the ALPHA keyboard when pressing [STO>].

The TI-84 Plus doesn't.

On to the programming...

TI-81 Decimal to Binary Conversion:  BINTODEC
(75 bytes)

Input the binary integer at the prompt.  Use only 1s and 0s. 

Variables:
B = binary representation
D = decimal representation
N, M:  used

Program:
Disp "BIN>DEC"
Input B
0 → D
0 → N
B → M
Lbl 0
2^N * 10 * FPart(M/10) + D → D
IPart(M/10) → M
IS>(N, IPart(log B) + 1)
Goto 0
Disp D

Example:
Input:  B:  1001010
Result:  D:  74

TI-81 Binary to Decimal Conversion:  DECTOBIN
(99 bytes)

Input the decimal integer at the prompt.  The integer needs to be in between 1 and 1024.  Only positive integers are allowed.

Variables:
B = binary representation
D = decimal representation
N, M:  used

Program:
Disp "DEC>BIN"
Disp "1≤D≤1024"
Input D
0 → B
D → M
IPart( log D / log 2 ) → N
Lbl 2
If 2^N ≤ M
B + 1 → B
If 2^N ≤ M
M - 2^N → M
If N ≠ 0 
10 * B → B
DS<(N, 0)
Goto 2
Disp B

Example:
Input:  D:  516
Result:  B:  1000000100

TI-81 Roots of a Quadratic Equation:  QUADEQN
(121 bytes)

This program solves the equation A*X^2 + B*X + C = 0, which allows for real or complex roots.

Variables:
A, B, C:  coefficients
X, Y:  roots

If the discriminant is zero or positive, the roots are real, and are stored in X and Y.

If the discriminant is negative, we have complex roots in the form of X ± Yi, X is the real part, Y is the imaginary part.

Program:
Disp "AX²+BX+C=0"
Input A
Input B
Input C
-B / (2A) → X
(B² - 4AC) / (4A²) → Y
If Y<0 font="">
Goto 0
√Y → Y
X + Y → Y
2X - Y → X
Disp "ROOTS"
Goto 2
Lbl 0
√(abs Y) → Y
Disp "X+YI, X-YI"
Lbl 2
Disp X
Disp Y

Examples:

x^2 + 4x + 5 = 0,  Roots:  2 ± i
Input:  A: 1, B: 4, C: 5
Results:  "X+YI, X-YI", X: -2, Y: 1

x^2 + 5x + 4 = 0,  Roots:  -4, -1
Input:  A: 1, B: 5, C: 1
Results: "ROOTS", X: -4, Y: -1

Tomorrow will be Part 2. Until then,

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Sunday, February 3, 2019

Calculator Programming: Making FOR, WHILE, and REPEAT Loops with IS>, DS<, GOTO, IF, LBL

Calculator Programming:  Making FOR, WHILE, and 
REPEAT Loops with IS>, DS<, GOTO, IF, LBL

Introduction

Say your programming capabilities on your calculator, BASIC portable computer, or programming app does not have the FOR, WHILE, and REPEAT loop structures.  No worries, there are some workarounds we can use.

Today the programs will be demonstrated with the language of the TI-81 and TI-84 Plus CE, but this blog isn't limited to these two calculators.


Simulating FOR Loops with IS> and DS<

The TI-81 (and every Texas Instruments graphing calculator of that family inclusive to the TI-84 Plus CE) has the commands IS> and DS<.

IS>:  Increment and Skip

Syntax:  IS>(variable, target number)

IS> increases the value stored in a variable by 1 and performs a comparison test.  The next command is excited if the increased value is less than or equal to the target number.

IS>(var, target)
[do this command if var + 1 ≤ target]
[skip to this command if var + 1 > target]

Simulating the FOR Loop:

FOR var = begin TO end*
[commands]
NEXT
[commands after loop is over]


* Syntax varies.  For the TI-84 Plus CE:  For(var,begin, end) : [commands] : End

with:

begin → var
LBL [label]
[commands]
IS>(var, end)
GOTO [label]
[commands after loop is over]

The program PRGM1 demonstrates how to accomplish a FOR loop with IS>.  PRGM1 displays the numbers 1 to 10.  The TI-81 section can be programmed by on the TI-81 and TI-84 Plus CE, which the TI-84+ section is for calculators TI-82 and later.

"TI-81"
1→K
Lbl 1
Disp K
IS>(K,10)
Goto 1
Disp "END"
Wait 2
"TI-84+"
For(K,1,10)
Disp K
End
Disp "END"

DS<   

Decrement and Skip

Syntax:  DS<(variable, target number)

DS< decreases the value stored in a variable by 1 and performs a comparison test.  The next command is excited if the increased value is greater than or equal to the target number.  DS< is the opposite of IS>.

DS<(var, target)
[do this command if var - 1 ≥ target]
[skip to this command if var - 1 < target]

Simulating the FOR Loop:

FOR var = begin TO end STEP -1*
[commands]
NEXT
[commands after loop is over]


* Syntax varies.  For the TI-82 to the TI-84 Plus CE, including TI-80 and TI-73:
For(var,begin, end, -1) [commands] End

with:

begin → var
LBL [label]
[commands]
DS<(var, end)
GOTO [label]
[commands after loop is over]

PRGM2 demonstrates the use of DS< and the associated For loop.


"TI-81"
10→K
Lbl 1
Disp K
DS<(K,1)
Goto 1
Disp "END"
Wait 2
"TI-84+"
For(K,10,1,­1)
Disp K
End
Disp "END"

Simulating WHILE and REPEAT Loops

With the proper use of the IF, LBL, and GOTO we can simulate WHILE and REPEAT loops.

Simulated WHILE Loops

WHILE [condition is true]
[commands]
END

* While is available for TI-82 to the TI-84 Plus CE, including TI-73.

can be simulated by:

LBL [label]
[commands]
IF [while condition] 
GOTO [label]

Simulated REPEAT Loops

REPEAT
[commands]
UNTIL [condition]

* The syntax for the TI-82 to the TI-84 Plus CE, including TI-73 is:
Repeat [condition] : [commands] : End

LBL [label]
[commands]
IF [inverse of the repeat condition] 
GOTO [label]

Example:  If the repeat condition is T>5, then the inverse is T≤5.

PRGM3 is a demonstration program on how all three techniques are used to accomplish this:

Start with 1000.  Take the square root.  Keep going until the calculator gets to 1 (by the precision of the calculator).

"TI-81"
1000→K
Lbl 1
√(K)→K
Disp K
If K≠1
Goto 1
Disp "END"
Wait 2
"84+ WHILE"
1000→K
While K≠1
√(K)→K
Disp K
End
Disp "END WHILE"
Wait 2
"84+ REPEAT"
1000→K
Repeat K=1
√(K)→K
Disp K
End
Disp "END REPEAT"

I hope you find these tips useful and helpful.

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Monday, January 21, 2019

HP Prime: Renaming Headers in Statistic Editors

HP Prime:  Renaming Headers in Statistic Editors

I recently received an email asking for help of how to rename columns of the Statistics Editor screen.  I was not able to figure it out myself, I then asked if anyone at the Museum of HP Calculators knew. 

Link to the HPMoC thread:  http://www.hpmuseum.org/forum/thread-12231.html

Tim Wesseman replied that you can change the header name in the statistic editor by using:

D1(-1) = " [ header name in a string ] "

This applies to D2 - D9, D0, C1 - C9, and C0.  The HP Prime uses D# in anaylzing 1 Variable Statistics and C# in analyzing 2 Variable Statistics.



In the illustration listed above, I renamed the headers for both C1 and C2 as "X DATA" and "Y DATA". 

C1(-1):="X DATA"
C2(-1):="Y DATA"

Note this only changes the header in the Numeric View of the Statistics apps (Statistics 1Var, Statistics 2Var).

Caution:  This only works for Statistics columns, not for list columns for the list editor, or the matrix columns and rows for the matrix editor.

Tyann states to clear the header, simply store an empty string.  In this example,

C1(-1):=""
C2(-1):=""

Thanks to Tim Wessman, Tyann, and Roger Céspedes Esteban to sending me the email (the best for your Descriptive Statistics app, Roger).

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Saturday, February 4, 2017

HP Prime: Base Conversions – Joe Horn

HP Prime:  Base Conversions – Joe Horn

Special thanks to Joe Horn.  When I posted the conversion programs ( http://edspi31415.blogspot.com/2017/02/hp-prime-and-ti-84-plus-ce-convert.html ), Horn alerted me to a hidden convert command where we can convert base integers.

The HP Prime has a CONVERT (upper case letters) that converts units (i.e. length, volume, etc.).  However, for the base to work, not only convert has to be typed in lower case letters, AND it is a CAS command.

The syntax that Joe told me is:

Decimal to Other Base (D2B)

convert(number in base 10, base, desired base)

The word base is typed in lowercase.  The result is a vector of digits but the order goes from lower power to upper power.  The number will be written backwards.  To get it in proper order:

REVERSE(convert(number in base 10, base, desired base))

In Home mode or in programming, you will need a CAS command attached to the syntax.

Other Base to Decimal

convert(vector of digits, base, base that is represented)

Again, going this way the vector of digits goes from lower to upper power.  The proper order requires the following:

convert(REVERSE(vector of digits), base, base that is represented)

Once again, in Home mode or in programming, you will need a CAS command attached to the syntax.

Below are programs that help accomplish these tasks:

HP Prime Program D2B (decimal to base)

EXPORT D2B(n,b)
BEGIN
// decimal to base
// Joe Horn
CAS("REVERSE(convert(n,base,b))");
END;

Example:  D2B(17291, 12) returns [10 0 0 11]   (A00B_12)

HP Prime Program B2D (base to decimal)

EXPORT B2D(n,b)
BEGIN
// vector (decr),base to base 10
// Joe Horn
CAS("convert(REVERSE(n),base,b)");

END;

Example:  B2D([1, 0, 1, 6, 2, 5], 7) returns 17463.  (101625_7)

Thank you Joe!  As Joe Horn always says, Happy Programming!

Eddie

This blog is property of Edward Shore, 2017.



Saturday, August 20, 2016

Internal Rate of Return and Polynomials

Internal Rate of Return and Polynomials 

The internal rate of return function (IRR) is featured on many financial calcualtors, such as the HP 12C and HP 10bII (Plus) from Hewlett Packard, and BA II Plus (Professional) from Texas Instruments.  It calculates an interest rate of a group of cash flows when the net present value (NPV) is zero.  

0 = CF0 + CF1/(1 + r%) + CF2/(1 + r%)^2 + CF3/(1 + r%)^3 + ...

where r% = r/100 and r = IRR. 

The IRR can also aid in finding a root of polynomial.  Let x = 1/(1 + r/100).  Then:

0 = CF0 + CF1*x + CF2*x^2 + CF3*x^3 + ...

In the case, the cash flows are coefficients of the polynomial.  When calculating IRR, enter cash flows (coefficients) in terms of coefficients of increasing powers of x. 

Once r is calculated, then we can calculate for x (the root):

Algebraic operating system (HP 10bII, BA II Plus, and most others):
IRR [ ÷ ] 100 [ + ] 1 [ = ] [1/x] 

Reverse Polish Notation (RPN) system (HP 12C, a setting on the HP 17B II+):
IRR 1 [ % ] 1 [ + ] [1/x]

Things to keep in mind:

1.  This method is for polynomials for real roots only.  If all the roots are complex, this method is a no-go. 
2. Likewise, no complex number coefficients.  
3. The IRR function and algorithm to adjust it to find the root will be a positive root that is closest to zero (not always, but this is the case most of the time).  

A beautiful article by Valentin Albillo covers this topic for the HP 12C calculator and has a program to find all the roots for polynomials: http://www.hpcc.org/datafile/V21N2/V21N2P35.pdf

Let's look at a few examples. For these examples, all results are rounded to four decimal places (FIX 4). 

Using IRR to Find a Root to a Polynomial 

Example 1:  x^2 + 2x - 3 = 0

Enter the cash flows:
CF0 = -3
CF1 = 2
CF2 = 1

If need be, set P/Y = 1 (not necessary for the HP 12C and BA II Plus (Professional))

Compute IRR to get 0.0000

Follow the transformation x = 1/(IRR/100 + 1) and one root of x^2 + 2x - 3 is:

x = 1.0000

To my knowledge, the only calculator that allows for initial guesses is the HP 12C by entering the initial guess, then pressing [RCL], [ g ], [R/S].  

Example 2: 11.2x^4 - 2.1x^3 + 3x^2 - 3.6x - 2 = 0

Set the cash flows as follows:
CF0 = -2
CF1 = -3.6
CF2 = 3
CF3 = -2.1
CF4 = 11.2

Calculating IRR yields 29.7291.  

Transforming to the root for the polynomial will yield the root 0.7708.  

Simple enough, but let's look at the next example. 

Example 3: x^2 + 10x + 24 = 0

The cash flows for this problem would be:

CF0 = 24
CF1 = 10
CF2 = 1

Straight forward so far.  However, calculating IRR will yield an error.  Why?

The IRR function will require that at least one cash flow be the opposite sign, a mix of negative and positive cash flows.  Looking at what we have, all the flows are positive.  We can make the problem more palpable, we can make another substitution x = -t. As a consequence:

x = -t, x^2 = t^2, x^3 = -t^3, x^4 = t^4, and so on.

Our transformed polynomial is now:  t^2 - 10t + 24 = 0.  

The adjusted cash flows are:

CF0 = 24
CF1 = -10
CF2 = 1


This will require an adjustment in transform from IRR to root:

x = -1/(IRR/100 + 1) 

Procedure if the polynomial is going to adjusted by this additional step:

Algebraic operating system (HP 10bII, BA II Plus, and most others):
IRR [ ÷ ] 100 [ + ] 1 [ = ] [1/x] [+/-]

Reverse Polish Notation (RPN) system (HP 12C, a setting on the HP 17B II+):
IRR 1 [ % ] 1 [ + ] [1/x] [CHS]

Now calculate IRR. 

The HP 10b II+ gives -83.3333.   Translating to the root -6.0000. 

The BA II gives -75.000, which translate to the root -4.0000. 

Both are correct. 

The HP 12C gives Error 3.  However, if I entered an initial guess of say, -50, by keying -50 [RCL], [ g ], [R/S] returns -75.0000.   (For the root of -4.0000 after converting IRR to x (see above)). 

Interesting to see how the IRR function can help find a root of polynomials, which the required adjustments. 

Using the Polynomial Solver to Find IRR

Let's go the other way.  Say we have a scientific, even graphing calculator, and we are going to need to find the IRR but we don't have such function?  

Recall:

0 = CF0 + CF1/(1 + r%) + CF2/(1 + r%)^2 + CF3/(1 + r%)^3 + ...

where r% = r/100 and r = IRR. 

The IRR can also aid in finding a root of polynomial.  Let x = 1/(1 + r/100).  Then:

0 = CF0 + CF1*x + CF2*x^2 + CF3*x^3 + ...

Solving x = 1/(1 + r/100) for r yields:  r = 100*(1/x- 1)

In this case (and ideally), pick the root (x) that is positive and the closet to zero. 

Example 4:  Find the IRR for the following cash flows:
CF0 = -5000
CF1 = 1000
CF2 = 2000
CF3 = 3000
CF4 = 5000

The translated polynomial to be solved is:
0 = -5000 + 1000x + 2000x^2 + 3000x^3 + 5000x^4

Using the PolySmlt2 app of the TI-84 Plus CE I get the following roots, rounded to four decimal places:
-0.1238 ± 1.0739i, -1.1179, 0.7655

Substituting x = 0.7655 into r = 100*(1/x- 1) yields an approximate IRR of 30.6336%.  

Running the problem through the financial calculators yield 30.6348%. Using more decimal places of x will get a more precise IRR. 

This is the relationship between IRR and roots of polynomials.  

Eddie

Source: Albillo, Valentin "HP-12C's Serendipitous Solver". Ex-PPC #4747, HPCC #1075, Datafile.  March/April 2002. http://www.hpcc.org/datafile/V21N2/V21N2P35.pdf



This blog is property of Edward Shore, 2016 

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues

DM42 and HP 42S: Quadratic Equation, Characteristic Polynomial, and Eigenvalues The programs are listed for the Swiss Micros DM42 an...