Re: WP34s contributed programs. Where to send? Message #11 Posted by Thomas Klemm on 24 May 2013, 7:28 p.m., in response to message #7 by Paul Dale
Arithmetic-Geometric Mean
This is a Python-script:
eps = 1e-14
def agm(y):
x = 1.0
while math.fabs(x - y) > eps:
x, y = (x + y)/2, math.sqrt(x * y)
return x
And that's the program for the HP-42S:
00 { 29-Byte Prgm }
01 LBL "AGM"
02 4
03 X<>Y
04 1
05 LBL 00
06 RCL+ ST Y
07 X<>Y
08 RCL* ST L
09 SQRT
10 X<>Y
11 2
12 /
13 DSE ST Z
14 GTO 00
Modified Arithmetic-Geometric Mean
I've made some modifications to the formula presented in the paper. Be aware that x and y correspond to x - z and y - z.
This is a Python-script:
eps = 1e-14
def magm(y):
x, z = 1.0, 0.0
while math.fabs(x - y) > eps:
w = math.sqrt(x * y)
x, y, z = (x + y)/2 + w, 2 * w, z - w
return y + z
And that's the program for the HP-42S:
00 { 37-Byte Prgm }
01 LBL "MAGM"
02 0
03 X<>Y
04 1
05 LBL 00
06 RCL+ ST Y
07 X<>Y
08 RCL* ST L
09 SQRT
10 STO- ST Z
11 STO+ ST X
12 STO+ ST Y
13 X<>Y
14 2
15 /
16 X#Y?
17 GTO 00
18 RDN
19 +
20 END
Perimeter of Ellipse
00 { 37-Byte Prgm }
01 LBL "ELIPER"
02 STO 00
03 /
04 STO 01
05 XEQ "AGM"
06 X<> 01
07 X^2
08 XEQ "MAGM"
09 RCL/ 01
10 PI
11 *
12 STO+ ST X
13 RCL* 00
14 END
Using the example (a = 3, b = 2) that Gerson used in
this other thread I got:
2 ENTER 3
XEQ "ELIPER"
15.8654395897
Kind regards
Thomas
Edited: 24 May 2013, 7:40 p.m.
|