Showing posts with label iterative process. Show all posts
Showing posts with label iterative process. Show all posts

Saturday, November 7, 2020

HP Prime: Approximate of a Quartic Root

 HP Prime: Approximate of a Quartic Root


Introduction


In the article "Square Root & Cube Root Algorithms" (see source below), teacher and educator Dave  Elgin wrote an article on how his Advanced Higher Applied Mathematics class developed algorithms to estimate the square root and cubic roots of numbers.  The algorithms are first based off of Netwon's Method, then uses selected initial guesses and solving for linear systems.


Elgin's Derivation 


Square Root


Suppose x^2 = k.   To set up for Newton's Method, let f(x) = x^2 - k, with df/dx = 2x.  Then


x_n+1 = x_n - (x_n^2 - k) / (2 * x_n) = 1/2 * (x_n + k / x_n^2)


(The article uses N for the number to find the root of, but to eliminate confusion, I use k.  Completely a choice of labels.)


The class used a pattern to determine guesses for the square root of k:


k / (x_n),  (k^2) / (x_n^3),  (k^3) / (x_n^5), and so on.   


Let g(x) be an iterative function where x_n+1 = g(x_n) and for a second-order approximation:


g(x) = a1 * x + a2 * k / x + a3 * k^2 / x^3


Two derivatives of g(x) are taken:


g'(x) = a1 - a2 * k / x^2 - 3 * a3 * k^2 / x^4, with g'(x_n) = 0


g''(x) = 2 * a2 * k / x^3 + 12 * a3 * k^2 / x^5 with g''(x_n) = 0


This requires the following system of equations to be solved for a1, a2, and a3 (after substituting x = √k):


1 = a1 + a2 + a3

0 = a1 - a2 - 3 * a3

0 = 2 * a2 + 12 * a3


leading to the solutions a1 = 3/8, a2 = 3/4, and a3 = -1/8


Hence the second-order recursive function for the square root is (after simplifying):


g(x) = 3/8 * x + (3 * k) / (4 * x) -  k^2 / (8 * x^3)


which translates to 


x_n+1 = 3/8 * x_n + (3 * k) / (4 * x_n) -  k^2 / (8 * x_n^3)


Cube Root


They repeat the same process for the cubic root, which I will briefly outline here:


x^3 = k,   f(x) = x^3 - k,  f'(x) = 3x^2


x_n+1 = x_n - (x^3 - k) / (3x^2) = 1/3 * (2 * x_n - k / (x_n^2))


With guess of k / (x_n^2) and k^2 / (x_n^5) used, the iterative function is set up as:


g(x) = a1 * x + a2 * k / x^2 + a3 * k^2 / x^5


and


g'(x) = a1 - 2 * a2 * k / x^3 - 5 * a3 * k^2 / x^6


g''(x) = 6 * a2 * k / x^4 + 30 * a3 * k^2 / x^7


and with g(x_n) = x_n+1, g'(x_n) = 0, g''(x_n) = 0 and substituting x = k^1/3, the system becomes:


1 = a1 + a2 + a3

0 = a1 - 2 * a2 - 5 * a3

0 = 6 * a2 + 30 * a3


with the solutions a1 = 5/9, a2 = 5/9, and a3 = -1/9, giving the second-order recursive function:


g(x) = 5/9 * x + (5 * k) / (9 * x^2) - k^2 / (9 * x^5)


The article shows the derivation of a third-order recursive function for both square and cube root. 


Deriving a Second-Order Algorithm for Quartic Roots


Let's use a similar approach used in Elgin's article to develop an algorithm to calculate the fourth (quartic) root:  


k^1/4 = x


Let f(x) = x^4 - k,  then f'(x) = 4*x^3, and


x_n+1 = x_n - (x_n^4 - k) / (4 * x_n^3) = 3/4 * x_n - k / (4 * x_n^3)


Use guesses x_n, k / (x_n^3), k^2 / (x_n^7), we set up the equations:


g(x) = a1 * x + a2 * k / x^3 + a3 * k^2 / x^7


g'(x) = a1 - 3 * a2 * k / x^4 - 7 * a3 * k^2 / x^8


g''(x) = 12 * a2 * k / x^5 + 56 * a3 * k^2 / x^9



Setting g(x) = x^1/4, g'(x) = 0, g''(x) = 0, and setting g(k^1/4), we get the system:


1 = a1 + a2 + a3

0 = a1 - 3 * a2 - 7 * a3

0 = 12 * a2 + 56 * a3


The solutions to above system:  a1 = 21/32, a2 = 7/16, a3 = -3/32, which gives the second order recursive  equation:


g(x) = 21/32 * x + (7 * k) / (32 * x^3) - (3 * k) / (32 * x^7)

 

The program FTHROOT use the recursive equation to approximate the quartic root. 



HP Prime Program:  FTHROOT


EXPORT FTHROOT(k)

BEGIN

// EWS 2020-10-21

// Approx 4th Root

LOCAL r,r0,r1,ri;

r:=k^0.25;

r0:=0; 

r1:=√k;

ri:=0;

WHILE ABS(r0-r1)>1ᴇ−10 DO

ri:=ri+1;

r0:=r1;

r1:=(21*r0)/32+(7*k)/(16*r0^3)-(3*k^2)/(32*r0^7);

END;

PRINT();

PRINT("4√"+PRINT(k));

PRINT("Root = "+STRING(r));

PRINT("------");

PRINT("Approximation: "+

STRING(r1));

PRINT("Iterations: "+STRING(ri));


END;


The choice of a good first guess is necessary with any iterative root finding process. The program FTHROOT chooses the square root of k for an initial guess.   The goal is to seek a positive root.


Examples


Each example is followed by a set of screen shots, which include setting up Sequences and their graphs on the HP Prime.  


Example 1


k = 176.4

Result:  3.64438831256 (algorithm took 7 iterations with initial guess √176.4)



Example 2

k = 5525
Result:  8.62150472576 (algorithm took 9 iterations with initial guess √5525)




Source

Elgin, Dave.  "Square Root & Cube Root Algorithms"  The Mathematical Association.  Mathematics in School, Jan. 2006, Vol. 35, No. 1 pp. 30-31.  https://www.jstor.org/stable/30215863

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, September 12, 2020

Casio fx-9750GIII: Happy Integers

Casio fx-9750GIII: Happy Integers


What Makes an Integer Happy?  

1.  Take a positive integer

2.  Take the sum of the squares of its integers

3.  Repeat the process until:


a.   You repeat a previous result or

b.   You obtain a sum of 1.  


In the cases that you eventually reach 1, that number is defined as a happy integer.


23:

2^2 + 3^2 = 13

1^2 + 3^2 = 10

1^2 + 0^2 = 1

23 is a happy integer.  


24:

2^2 + 2^2 = 4

4^2 = 16

1^2 + 6^2 = 37

3^2 + 7^2 = 58

5^2 + 8^2 = 89

8^2 + 9^2 = 145

1^2 + 4^2 + 5^2 = 42

4^2 + 2^2 = 20

2^2 + 0^2 = 4    (see first addition - repeat)

24 is not a happy integer.

The sequence of sums of squares of the integer's digits is known as a cherry sequence.  


Casio fx-9750GIII: HAPPY

(316 bytes)


"2020-08-20 EWS"

"N>0, INTEGER: N"? → N

{ 0 } → List 1

Intg log N → D

N ÷ 10^D → W

Lbl 3

0 → T

For 0 → I To D

T + (Intg W)^2 → T

(W - Intg W) × 10 → W

Next

Augment(List 1, { T }) → List 1

ClrText

Locate 1, 4, T

For 1 → I To 100

Next

T = 1 ⇒ Goto 1

For 1 → I To Dim List 1 - 1

T = List 1[ I ] ⇒ Goto 2

Next

T → W

Intg log W → D

W ÷ 10^D → W

Goto 3

Lbl 1

ClrText

Locate 1, 3, W

Locate 1, 4,"IS A HAPPY INTEGER"

Stop

Lbl 2

Locate 1, 3, N

Locate 1, 4, "NOT A HAPPY INTEGER"

Stop


Notes:

(1)  10^ is shown as a subscript 10 on the calculator.

(2)  The program sequence

ClrText

Locate 1, 4, T

For 1 → I To 100

Next

creates a timer.  This allows the calculator to show intermediate results for a short time.

(3)  Intermediate sums of squares are stored in List 1.  Other than the first 0 (an element is required to start a list in Casio programming), the rest of the sequence is known as Cheery Sequence.


Examples

N = 19;  Happy Number

N = 77; Not a Happy Number

N = 230; Happy Number

N = 562; Not a Happy Number 


Sources:

Duncan, Donald C.  Happy Integers.  The Mathematics Teacher, Vol 65. No. 7  November 1972, pp. 627-629  https://www.jstor.org/stable/27959021 

Happy Number.  GeeksforGreeks.  https://www.geeksforgreeks.org/happy-number  Updated February 4, 2020.  Accessed August 19, 2020.  (website under maintenance)

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. 


Sunday, July 26, 2020

HP 41C, HP 42S, TI-60: Arithmetic-Geometric Mean

HP 41C, HP 42S, TI-60:  Arithmetic-Geometric Mean



Arithmetic-Geometric Mean

The program AGM calculates the arithmetic-geometric mean of two positive integers x and y.   As the graphic above suggests, an iterative process is used to find the AGM, computing both the arithmetic mean and geometric mean until the two means converge.

a0 = x
g0 = y

Repeat:
Arithmetic Mean:  a1 = (a0 + g0)/2
Geometric Mean:  g1 = √(a0 * g0)
Transfer new to old:  a0 = a1, g0 = g1
Until |a1 - g1| < tolerance

You can set the tolerance as low as you want.  The programs presented on this blog set tolerance at 10^(-10)  (1E-10), to fit the calculator's display.

HP 41C Program: AGM

01 LBL^T AGM
02 STO 01
03 X<>Y
04 STO 02
05 X<>Y
06 LBL 00
07 RCL 02
08 RCL 01
09 ENTER
10 R↑
11 R↑
12 X<>Y
13 R↓
14 ENTER
15 R↑
16 +
17 2
18 /
19 STO 01
20 R↓
21 *
22 SQRT
23 STO 02
24 R↑
25 -
26 ABS
27 1E-10
28 X≤Y?
29 GTO 00
30 CLA
31 ^T AGM = 
32 ARCL 01
33 AVIEW
34 END

HP 42S/Swiss Micros DM42/Free42 Program AGM:

00 {53-Byte Prgm}
01 LBL "AGM"
02 STO 01
03 X<>Y
04 STO 02
05 X<>Y
06 LBL 00
07 RCL 02
08 RCL 01
09 ENTER
10 R↑
11 R↑
12 X<>Y
13 R↓
14 ENTER
15 R↑
16 +
17 2
18 /
19 STO 01
20 R↓
21 *
22 SQRT
23 STO 02
24 R↑
25 -
26 ABS
27 1E-10
28 X≤Y?
29 GTO 00
30 CLA
31 "AGM = "
32 ARCL 01
33 AVIEW
34 END

The instructions for both the HP 41C and 42S versions are same:  enter X and Y on the respective stacks and XEQ AGM.

Example (ALL/STD mode is applied):

AGM(37, 78): 
37, 78, XEQ AGM returns:
Alpha:  AGM = 55.5947005279

TI-60 Program: AGM

Instructions:

1.  Store X in memory register 1 and Y in memory register 2.
2.  Press [ RST ] [ R/S ], the value of |a1 - g1| is displayed.
3.  Keep on press [ R/S ] to repeat the calculation until |a1 - g1| falls under 10^(-10).
4.  Recall either memory register 1 or 2 to get the answer.

Registers needed: 1 - 4.

Step;  Key Code; Key
00;  71;  RCL
01;  01;  1
02;  85;  +
03;  71;  RCL
04;  02;  2
05;  95;  =
06;  55;  ÷
07;  02;  2
08;  95;  =
09;  61;  STO 
10;  03;  3
11;  71;  RCL
12;  01;  1
13;  86;  √
14;  65;  ×
15;  71;  RCL
16;  02;  2
17;  86;  √
18;  95;  =
19;  61;  STO
20;  04;  4
21;  71;  RCL
22;  03;  3
23;  61;  STO
24;  01;  1
25;  75;  -
26;  71;  RCL
27;  04;  4
28;  61;  STO
29;  02;  2
30;  95;  =
31;  87;  |X|
32;  13;  R/S

Example:

AGM(37, 78)
37 STO 1
78 STO 2
RST R/S

3.778495926, R/S
0.032100702, R/S
0.000002317, R/S
2 -11  (stop)

RCL 1 (or RCL 2):  55.59470053

Source:
"Arithmetic-geometric mean"  Wikipedia.  https://en.wikipedia.org/wiki/Arithmetic–geometric_mean  Last Edited June 12, 2020.  Accessed June 12, 2020.


Onward to August...

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