Tampilkan postingan dengan label Swiss Micros. Tampilkan semua postingan
Tampilkan postingan dengan label Swiss Micros. Tampilkan semua postingan

Swiss Micros DM42, Free42, Plus42: FUNC command

Swiss Micros DM42, Free42, Plus42:   FUNC command


Turning Programs into User Functions


The FUNC command saves the stack contents and treats the program like it is a function.  This allows the program to work with a temporary stack which is "deleted" when the program reaches either END or RTN (and RTNYES, RTNN), while storing the original contents of the X stack in the L (LAST X) stack.  


FUNC requires a two digit argument.  The first digit is the stack levels used in input, while the second digit is the stack levels used in output.


FUNC 11:  Takes the argument from the X stack and returns the answer in X stack, while leaving the rest of the stack intact.

X:  f(x),  Y: y0,  Z: z0,  T:  t0,  Last X:  x0


FUNC 21:  Takes the arguments from the X and Y stacks and returns the answer in the X stack.

X:  f(x,y),  Y:  z0, Z: t0, T: t0, Last X: x0


FUNC22:  Takes the arguments from the X and Y stacks and returns the answers in the X and Y stacks.

X:  x from f(x,y),  Y:  y from f(x,y), Z:  z0,  T:  t0, Last:  x0


Example 1:  f(x,y) = (y + x)/(y - x)


Stack set up:

T = 24

Z = 15

Y = 45

X = 21


PR1:  Program 


00 { 17-Byte Prgm }

01▸LBL "PR1"

02 -

03 ENTER

04 ENTER

05 LASTX

06 +

07 LASTX

08 +

09 X<>Y

10 ÷

11 RTN

12 END


BEFORE PR1:

T=               70.0000

Z=               15.0000

Y=               45.0000

X=               21.0000


AFTER PR1:

T=               15.0000

Z=               15.0000

Y=               15.0000

X=                2.7500


FN1:  Using FUNC21


00 { 20-Byte Prgm }

01▸LBL "FN1"

02 FUNC 21

03 -

04 ENTER

05 ENTER

06 LASTX

07 +

08 LASTX

09 +

10 X<>Y

11 ÷

12 RTN

13 END


BEFORE FN1:

T=               70.0000

Z=               15.0000

Y=               45.0000

X=               21.0000


AFTER FN1: 

T=               70.0000

Z=               70.0000

Y=               15.0000

X=                2.7500



Example 2:  f(x,y) = x * y * √(x^2 + y^2)


Stack set up:

T = 64

Z = 11

Y = 15

X = 25



Note: The →POL command calculates that  x^2 + y^2 on the x stack


PR2:  Program 


00 { 17-Byte Prgm }

01▸LBL "PR2"

02 STO ST Z

03 X<>Y

04 STO× ST Z

05 →POL

06 RCL× ST Z

07 RTN

08 .END.


BEFORE PR2:

T=               64.0000

Z=               11.0000

Y=               15.0000

X=               25.0000


AFTER FN2:

T=               64.0000

Z=              375.0000

Y=                1.0304

X=           10,933.0348


FN2:  Using FUNC21


00 { 20-Byte Prgm }

01▸LBL "FN2"

02 FUNC 21

03 STO ST Z

04 X<>Y

05 STO× ST Z

06 →POL

07 RCL× ST Z

08 RTN

09 END


BEFORE FN2:

T=               64.0000

Z=               11.0000

Y=               15.0000

X=               25.0000


AFTER FN2:

T=               64.0000

Z=               64.0000

Y=               11.0000

X=           10,933.0348



As we can see, the FUNC command is a powerful command that allows programs to act like user functions, and is a great function for the DM42, Free42, and Plus42.  Unfortunately, FUNC is not available on the original HP 42S.


Until next time, happy programming,


Eddie 


All original content copyright, © 2011-2022.  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: Recursive Fibonacci by Marko Draisma

DM42:  Recursive Fibonacci by Marko Draisma


The programs on today's entry is provided from Marko Draisma and they are provided with permission and a request.  Gratitude to Marko.


DM42 (and Free42/Plus42) Program: FIBO (Marko Draisma)


This recursive program generates the nth Fibonacci number.  FIBO is a recursive program because it calls itself in the program steps.


LBL "FIBO"

LSTO "N"

X>0?

GTO 00

0

RTN

LBL 00

1

RCL "N"

X>Y?

GTO 01

1

RTN

LBL 01

RCL "N"

1

-

XEQ "FIBO"

LSTO "M"

RCL "N"

2

-

XEQ "FIBO"

RCL "M"

+

END



1st Fibonacci number:  1 FIBO returns 1

2nd Fibonacci number:  2 FIBO returns 1

3rd Fibonacci number:  3 FIBO returns 2

4th Fibonacci number:  4 FIBO returns 3

5th Fibonacci number:  5 FIBO returns 5

6th Fibonacci number:  6 FIBO returns 8

7th Fibonacci number:  7 FIBO returns 13


The LSTO Command


This is the local store command.  Local variables are created only for the purpose of the program and are deleted when the program hits either a return command (RTN) or an end command (END).  


Variables created by the LSTO command are named variables.  We cannot create local variables to numeric registers.   There is no storage arithmetic with LSTO.  


LSTO is available for the Swiss Micros DM42, Free42, and the Plus42.


Execution Time of FIBO


The execution time of FIBO depends on how large the entry n. Calculating the 50th Fibonacci number, 12,586,269,025, takes over 12 minutes. Marko provides a fast tail recursion version, as presented next.  


DM42 (and Free42/Plus42) Program: FIBO2 (Marko Draisma)


00 { 80-Byte Prgm }

01▸LBL "FIBO2"

02 FS? 01

03 GTO 01

04 LSTO "N"

05 1

06 LSTO "B"

07 0

08 LSTO "A"

09 SF 01

10 XEQ "FIBO2"

11 RTN

12▸LBL 01

13 RCL "N"

14 X≠0?

15 GTO 02

16 CF 01

17 RCL "A"

18 RTN

19▸LBL 02

20 RCL "A"

21 ENTER

22 RCL+ "B"

23 LSTO "A"

24 X<>Y

25 LSTO "B"

26 RCL "N"

27 1

28 -

29 LSTO "N"

30 XEQ "FIBO2"

31 END


Getting the 50th Fibonacci number with FIBO2 is snap.   


Thanks once again to Marko Draisma.


See you next time,


Eddie 


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


HP and Swiss Micros 41C and 42S Series: Stack Storage Operations

HP and Swiss Micros 41C and 42S Series: Stack Storage Operations


Introduction  


The HP 41C and HP 42S families, including the Swiss Micros DM41, DM42, Free42, and Plus42, includes the ability to execute storage arithmetic on any of the four* stack levels X, Y, Z, and T.  


The storage arithmetic operator takes whatever value is in the X stack and applies it to any stack level.  If we enter a number before the stack arithmetic operation, the stack lifts first, then executes the stack arithmetic operation.   See the examples section for illustrations.  


*Some calculators allow for a bigger number of stack levels, like 8.  



Examples



Example 1:  


Keystrokes:  3 [ ENTER ] [ ENTER ] 1 [ ENTER ]

Stack:

T:  3

Z:  3

Y:  1

X:  1


Keystrokes:  41:  [ STO ] [ + ] [ . ] (Y)    42:  [ STO ] [ + ] [ . ]  (ST Y)

Stack:

T:  3

Z:  3

Y:  2

X:  1


Keystrokes:  3 

Stack:

T:  3

Z:  2

Y:  1

X:  3_



Keystrokes:   41:  [ STO ] [ + ] [ . ] (Y)    42:  [ STO ] [ + ] [ . ]  (ST Y)

Stack:

T:  3

Z:  2

Y:  4

X:  3



Example 2:  


Keystrokes:  3 [ ENTER ] [ ENTER ] 1 [ ENTER ]

Stack:

T:  3

Z:  3

Y:  1

X:  1


Keystrokes:  41:  [ STO ] [ + ] [ . ] (Z)    42:  [ STO ] [ + ] [ . ]  (ST Z)

Stack:

T:  3

Z:  4

Y:  1

X:  1


Keystrokes:  3 

Stack:

T:  4

Z:  1

Y:  1

X:  3_



Keystrokes:   41:  [ STO ] [ + ] [ . ] (Z)    42:  [ STO ] [ + ] [ . ]  (ST Z)

Stack:

T:  4

Z:  4

Y:  1

X:  3



Note:  Casio fx-991EX Week - September 5, 2022 to September 9, 2022 


Eddie 


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


Swiss Micros DM41X and HP 41C: Numeric Derivatives

Swiss Micros DM41X and HP 41C:  Numeric Derivatives


Introduction


The program DFX calculates one of three types of derivative:


1.  Normal Derivative   (DX)

2.  Logarithmic Derivative (LN)

3.  Power Derivative (PWR)


Where:


Normal Derivative:  f'(x)


Logarithmic Derivative:  d/dx( ln f(x) ) = f'(x) / f(x)  


Power Derivative:  d/dx( f^n(x) ) = n * f^(n-1)(x) * f'(x),  n doesn't have to be an integer


where f'(x) is estimated by:


f'(x) ≈ ( f(x + h) - f(x) ) / h


Variables:  x, h


The program uses a subroutine FX, see the examples for details.  


DM41X and HP 41C Program:  DFX


Uses program FX as a subroutine as f(x).


01 LBL ^T FX

02 RCL 01

03 ^T X?

04 ARCL 01

05 PROMPT

06 STO 01

07 RCL 02

08 ^T H?

09 ARCL 02

10 PROMPT

11 STO 02

12 RCL 01

13 XEQ ^T FX

14 RCL 03

15 RCL 01

16 RCL 02

17 +

18 XEQ ^T FX

19 RCL 03

20 -

21 RCL 02

22 /

23 STO 04

24 ^T 1 DX 2 LN 3 ↑N

25 PROMPT

26 INT

27 STO 05

28 GTO IND 05


29 LBL 01

30 RCL 04 


31 LBL 02

32 RCL 04

33 RCL 03

34 / 

35 GTO 04


36 LBL 03

37 RCL 03

38 ^T N?

39 PROMPT

40 1

41  - 

42 Y↑X

43 LASTX

44 1

43 +

44 *

45 RCL 04

46 *


50 LBL 04

51 RTN



The function FX:


x is loaded in the X stack register (and on display)


01 LBL ^FX

02  execute f(x)

...

##  RTN


For DFX, do not use R01, R02, R03, R04, and R05 in FX.  


Notes:


Sequences such as:

02 RCL 01

03 ^T X?

04 ARCL 01

05 PROMPT


Puts the prompt as X? [contents of R1].   If you want the previous value, press R/S.  Otherwise enter a new value, then press R/S.


This program uses indirect goto statements.  R05 is used to hold the person's choice and uses it to direct which label is executed.


Examples


Example 1:  f(x) = x * sin x


Set FX as:

01 LBL^T FX

02 RAD

03 ENTER

04 ENTER

05 SIN

06 * 

07 RTN


Setting H to 10^-6  (1E-6):


DF:   f'(x)

x = 0.5, Result:  0.9182; x = 1.6, Result: 0.9530


LN:  ln f'(x)

x = 0.5, Result:  3.8305; x = 1.6, Result:  0.5959


PWR: with n = 3,   (f'(x))^3

x = 0.5; Result:  0.1583; x = 1.6;  Result: 7.3128



Example 2:  f(x) = x^2 + 3 * x + 1 = x * (x + 3) + 1


Set FX as:

01 LBL^T FX

02 ENTER

03 ENTER

04 3

05 +

06 *

07 1

08 + 

09 RTN


Setting H to 10^-6  (1E-6):


DF:   f'(x)

x = 3.2, Result:  9.4000; x = 6.8, Result: 16.6000


LN:  ln f'(x)

x = 3.2, Result:  0.4511; x = 6.8, Result: 0.2454


PWR: with n = 1.5,   (f'(x))^1.5

x = 3.2, Result:  64.3677; x = 6.8, Result: 204.7864



Until next time, 


Eddie


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


Backlink 9999 Traffic Super

Order Now...!!!!