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.