Showing posts with label Horner's Method. Show all posts
Showing posts with label Horner's Method. Show all posts

Sunday, March 5, 2023

Radio Shack EC-4000 (equiv. of TI-57): Program Library

Radio Shack EC-4000 (equiv. of TI-57):   Program Library





Radio Shack EC-4000/TI-57:  Horner's Method


Let's start with a demonstration of Horner's Method, which allows a quick evaluation of polynomials.   For a quadratic polynomial:


p(x) = a * x^2 + b * x + c = x * (a * x + b) + c



The program demonstrates an example polynomial:


p(x) = 3 * x^2 - 4 * x + 9 = x * (3 * x -  4) + 9



Step:  Key Code [ Key ]

00: 32, 1 [ STO 1 ]

01:  55 [ × ]

02:  03  [ 3 ]

03:  65  [ - ]

04:  04 [ 4 ]

05:  85  [ = ]

06:  55 [ × ]

07:  33, 1 [ RCL 1 ]

08:  75  [ + ]

09:  09  [ 9 ]

10:  85 [ = ]

11:  81  [ R/S ]

12:  71  [ RST ]


Examples:

p(0) = 9

p(5) = 64

p(9) = 216


Horner's Method can apply to higher order polynomials.  




Radio Shack EC-4000/TI-57:  Permutation and Factorial



This is to add two of the common probability functions that are missing with on the calculator.  


nPr = n! / (n - r)!


If n = r, then nPn = n!


Store n in R0 (register 0), r in R1 (register 1), then run the program (RST, R/S).


Other registers used:  R2:  nPr,  R7:  n-r+1 


Note that R7 is also the t-register.  


Step:  Key Code [ Key ]

00:  01  [ 1 ]

01:  32, 2  [ STO 2 ]

02:  33, 0  [ RCL 0 ]

03:  65   [ - ]

04:  33, 1  [ RCL 1 ]

05:  75  [ + ]

06:  01  [ 1 ]

07:  85  [ = ]

08:  32, 7 [ STO 7 ]

09:  86, 5 [ LBL 5 ]

10:  33, 0 [ RCL 0 ]

11:  39, 2  [ Prd 2 ]

12:  01  [ 1 ]

13:  -34, 0 [ INV SUM 0 ]

14:  33, 0 [ RCL 0 ]

15:  76  [ x≥t ]

16:  51, 5 [ GTO 5 ]

17:  33, 2 [ RCL 2 ]

18:  81  [ R/S ]

19:  71  [ RST ]



Examples:

n = 6, r = 6:  6P6 = 6! = 720

n = 5, r = 3:  5P3 = 60

n = 11, r = 6:  11P6 = 332,640

n = 52, r = 5:  52P5 = 3.118752 * 10^8



Radio Shack EC-4000/TI-57:  Snell's Law


There are two subroutines in this program listing.  Subroutines accessed by the [ SBR ] key.  


Snell's Law describes, among other things, the relationship between index of refraction of a medium (n) and refraction angle of the direction of light (Θ) is stated by:


n1 * sin(Θ1) = n2 * sin(Θ2)



Subroutine 1:  Solve for n2.  n2 = (n1 * sin(Θ1)) / sin(Θ2)


Subroutine 2:  Solve for Θ2.  Θ2 = arcsin( n1 * sin(Θ1) / n2 )


The registers used are:  R1 = n1, R2 = n2,  R3 = Θ1, R4 = Θ2


Step:  Key Code [ Key ]


// Solve for n2

00:  86, 1  [ LBL 1 ]

01:  50  [ DEG ]

02:  33, 1 [ RCL 1 ]

03:  55  [ × ]

04:  33, 3  [ RCL 3 ]

05:  28  [ SIN ]

06:  45  [ ÷ ]

07:  33, 4 [ RCL 4 ]

08:  28  [ SIN ]

09:  85  [ = ]

10:  32, 2 [ STO 2 ]

11:  81  [ R/S ]


// Solve for Θ2

12:  86, 2  [ LBL 2 ]

13:  50  [ DEG ]

14:  33, 1 [ RCL 1 ]

15:  55  [ × ]

16:  33, 3 [ RCL 3 ]

17:  28  [ SIN ]

18:  45  [ ÷ ]

19:  33, 2 [ RCL 2 ]

20:  85 [ = ]

21:  -28  [ INV SIN ]

22:  32, 4 [ STO 4 ]

23:  81 [ R/S ]


Examples:


Solve for n2:  n1 = 1.000293 (air), Θ1 = 30°, Θ2 = 19°

1.000293 STO 1

30 STO 3

19 STO 4

GSB 1

Result:  n2:  1.5362267


Solve for Θ2:  n1 = 1.000293 (air), Θ1 = 30°, n2 = 1.333 (water)

1.000293 STO 1

30 STO 3

1.333 STO 2

GSB 2

Result:  Θ2:  22.036902



Radio Shack EC-4000/TI-57:  Pseudorandom Number Generation


This program attempts to generate random numbers using the generator:


x_n+1 = frac(997 * x_n + π)


The random numbers are between 0 and 1.  An initial seed will be required 


The calculator does not have fraction and integer parts, so they have to be simulated:


int(x) ≈ x + 1E10 - 1E10

frac(x) = x - int(x), will require a register 


See steps 10 to 23.  


Due to the internal 10 digits and the simulations, expect possible roundoff errors as random numbers are continuously generated.  


Step:  Key Code [ Key ]

00:  55 [ × ]

01:  09  [ 9 ]

02:  09  [ 9 ]

03:  07 [ 7 ]

04:  75  [ + ]

05:  30  [ π ]

06:  85  [ = ]

07:  32, 6 [ STO 6 ]

08:  75  [ + ]

09:  01  [ 1 ]

10:  42  [ EE ]

11:  01  [ 1 ] 

12:  00 [ 0 ]

13:  65  [ - ]

14:  01  [ 1 ]

15:  42  [ EE ]

16:  01  [ 1 ] 

17:  00 [ 0 ]

18:  85 [ = ]

19:  -42  [  INV EE ]

20:  -34,6  [ INV SUM 6 ]

21:  33, 6 [ RCL 6 ]

22:  81  [ R/S ]

23:  71  [ RST ]



Example:


Seed:  0.98

Generation:

0.2015927

0.1294647

0.2178986

0.3864470

0.4292517



Until next time,


Eddie 


All original content copyright, © 2011-2023.  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, May 31, 2020

HP12C: Rational Fractions and Horner's Method

HP12C: Rational Fractions and Horner's Method

Introduction

Here are three examples on how Horner's Method can be used to quickly calculate rational fractions with polynomials.   The program code is presented for the HP 12C.

Horner's rule involves repeated factoring until the polynomial is represented as a multiplication of polynomials.  The idea is to make it easier for some scientific calculators and four-function calculators to evaluate polynomials.  Using Horner's Method for the generic cubic polynomial:

a * t^3 + b * t^2 + c * t + d
t * (a * t^2 + b * t + c) + d
t * (t * (a * t + b) + c ) + d

On an RPN keystroke calculator, such as the HP 12C a possible code would look like:

STO  t   (from the X stack)
RCL a
*
RCL b
+
RCL t
*
RCL c
+
RCL t
*
RCL d
+
RTN

For the code below, the HP 12C uses the following registers:

R0 = x

R1 = a
R2 = b
R3 = c
R4 = d
R5 = e
R6 = f
R7 = g

All but x need to be stored ahead of time before running (you can change the code to suit your needs, of course).  x is entered before pressing [ R/S ].

For all of our numerical examples, I assigned the following values:

R0 = 1.72

R1 = 6
R2 = 3
R3 = 4
R4 = 5
R5 = 3
R6 = 1
R7 = 8

Example 1

(ax + b) / (cx^2 + dx + e) = (ax + b) / (x * (cx + d) + e)

Program  (key:  key code) - 16 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.5684

Example 2

(ax + b) / (cx^3 + dx^ 2 + ex + f) = (ax + b) / (x * (x * (cx + d) + e) + f)

Program  (key:  key code) - 20 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 6:  45, 6
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.3225

Example 3

(ax^2 + bx + c) / (dx^3 + ex^2 + fx + g)
= (x * (ax + b) + c) / (x * (x * (dx + e) + f) + g)

Program  (key:  key code) - 24 steps

STO 0:  44, 0
RCL 1:  45,1
  *   :  20
RCL 2:  45, 2
  +  :  40
RCL 0:  45, 0
RCL 3:  45, 3
  *  :   20
RCL 4:  45, 4
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 5:  45, 5
  +  :  40
RCL 0:  45, 0
  *  :  20
RCL 6:  45, 6
  +  :  40
RCL 0:  45, 0
  *   :  20
RCL 7:  45, 7
  +  :  40
  ÷  :  10
GTO 00:  43, 33, 00

Result with variables stored above (FIX 4):  0.6111


Eddie

All original content copyright, © 2011-2020.  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, April 18, 2020

HP 12C: Sums of x

HP 12C:  Sums of x

Introduction

The following are four programs to calculate:

*  ∑ x
*  ∑ x^2
*  ∑ x^3
*  ∑ ax + b

with limits x = 0 to n.

Each program uses a closed sum formula.  The second and fourth program uses Horner's Method. 

Note:  You can use the first three if your limits are x = 1 to n (because at x = 0, 0 is added to the sum).  However if you use the fourth program, to get the sum from 1 to n, subtract b. 

HP 12C Program:  ∑x (x=0 to n)

n
∑ x =  n * (n + 1) / 2
x=0

Enter n, execute program

STEP:  KEY CODE  KEY
01:  36  ENTER
02:  36  ENTER
03:  1  1
04:  40  +
05:  20  *
06:  2   2
07  10  ÷
08:  43,33,00  GTO 00

Keys Only:
ENTER
ENTER
1
+
2
*
÷
GTO 00

Example:  (n = 48)

48
∑ x  = 1176
x=0

HP 12C Program:  ∑x^2 (x=0 to n)

n
∑ x^2 =  n * ( ( n / 3 + 1 / 2 ) * n + 1 / 6 )
x=0

Enter n, execute program

STEP:  KEY CODE KEY
01:  36  ENTER
02:  36  ENTER
03:  36  ENTER
04:  3  3
05:  10  ÷
06:  2  2
07:  22  1/x
08:  40  +
09:  20  *
10:  6  6
11:  22  1/x
12:  40  +
13:  20  *
14:  43,33,00  GTO 00

Keys Only:
ENTER
ENTER
ENTER
3
÷
2
1/x
+
*
6
1/x
+
*
GTO 00

Example:  (n = 48)

48
∑ x^2  = 38,024
x=0

HP 12C Program:  ∑x^3 (x=0 to n)

n
∑ x^3 =  1 / 4 * (n^2 + n)^2
x=0

Enter n, execute the program

STEP:  KEY CODE  KEY
01:  36  ENTER
02:  36  ENTER
03:  2  2
04:  21  y^x
05:  40  +
06:  2:  2
07:  21  y^x
08:  4  4
09:  10  ÷
10:  43,33,00  GTO 00

Keys Only:
ENTER
ENTER
2
y^x
+
2
y^x
4
÷
GTO 00

Example:  (n = 32)

32
∑ x^3  = 278,784
x=0

HP 12C Program:  ∑(a*x + b)  (x=0 to n)

n
∑ (a*x + b) =  n * (a * n / 2 + (a / 2 + b) ) + b
x=0

Store a in R1, store b in R2, enter n on the x-stack, execute the program

STEP:  KEY CODE   KEY
01:  36   ENTER
02:  36   ENTER
03:  36   ENTER
04:  45, 1  RCL 1
05:  20   *
06:  2    2
07:  10   ÷
08:  45, 1  RCL 1
09:  2   2
10:  10   ÷
11:  45, 2   RCL 2
12:  40   +
13:  40   +
14:  20   *
15:  45, 2  RCL 2
16:  40  ÷
17:  43, 33, 00   GTO 00

Keys Only:
ENTER
ENTER
ENTER
RCL 1
*

÷
RCL 1
2
÷
RCL 2
+
+
*
RCL 2
÷
GTO 00

Example:  (a = R1 = 3,  b = R2 = 4, n = 32)

3 STO 1,  4 STO 2,  32, R/S

32
∑ (3x + 4) = 1,716
x=0


I tried something new with my RPN keystroke programs, the second versions of each I just listed what keys to press without the step number or code.  I think this would be easier to read, but does leave out the key and key code.  Any comments on which style is preferred is appreciated. 


Eddie

All original content copyright, © 2011-2020.  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.

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...