Saturday, February 8, 2020

HP 42S and HP Prime: Rabbits vs Foxes

HP 42S and HP Prime: Rabbits vs Foxes

Introduction

The program presented today is based on the Rabbits vs. Foxes program for the HP 25 (see source below).  The program determines the population of rabbits and foxes over time as modeled by the differential equations:

Change in Rabbits:
dr/dt = 2 * r - α * r * f

Change of Foxes:
df/dt = -f + α * r  * f

where:
r = population of rabbits
f = population of foxes
α = probability of a rabbit encounters a fox
h = step 

This is approximated by Euler's method.

The HP Prime program RABBIT25 displays a print screen of results of the desired amount of iterations.  The HP 42S program RAB25 displays the results one step at time in the format rrrrr.fffff  (rabbits.foxes), like the original HP 25 program.


HP Prime Program RABBIT25

EXPORT RABBIT25()
BEGIN
// 2020-01-20 EWS
// Based on the Rabbits vs
// Foxes HP 25 program
LOCAL α,h,r,f,k,n,a;

// initialize and input
INPUT({α,h,r,f,n},
"Rabbits vs Foxes",
{"α: ","h: ","r0:","f0:","n: "},
{"α","h","inital # rabbits",
"initial # foxes",
"# iterations"});
L0:={r}; L1:={f};


// compute data
MSGBOX("L0 = rabbit population,
L1 = fox population;
(0,1,2,...,n), size n+1");
HFormat:=0;
PRINT();
PRINT("Rabbits vs Foxes");
FOR k FROM 0 TO n DO
IF k≠0 THEN
a:=α*r*f;
r:=r+h*(2*r-a);
f:=f+h*(−f+a);
END;
PRINT(k+" R: "+IP(r)+" F: "+
IP(f));
END;


// end of program
END;


HP 42S/Free42/DM42 Program RAB25

00 { 58-Byte Prgm }
01▸LBL "RAB25"
02 STO 02
03 R↓
04 STO 03
05 FIX 05
06▸LBL 05
07 RCL 02
08 ENTER
09 ENTER
10 RCL 03
11 RCL 00
12 ×
13 ×
14 STO 04
15 X<>Y
16 -
17 RCL 01
18 ×
19 +
20 X<0 font="">
21 0
22 STO 02
23 RCL 03
24 2
25 ×
26 RCL 04
27 -
28 RCL 01
29 ×
30 RCL 03
31 +
32 X<0 font="">
33 0
34 STO 03
35 IP
36 RCL 02
37 IP
38 1ᴇ5
39 ÷
40 +
41 STOP
42 GTO 05
43 .END.

Note:  You can replace STOP with PSE, as many as you like, if you don't want to press R/S after each step. 

Instructions:
Store α in R00, store h in R01, enter initial rabbit population

Example

Initial Populations:
Rabbits:  r = 300
Fixes:  f = 150
α = 0.01
h = 0.02

Iter:   Rabbits.Foxes  (rrrrr.ffff)
0:  300.00150
1:  303.00156
2:  305.00162
3:  307.00169
4:  309.00176
5:  311.00183
6:  312.00191
7:  312.00199
8:  312.00207
9:  312.00216
10: 311.00225
11: 309.00235
12: 307.00245
13: 304.00255
14: 301.00265
15: 297.00276


Source:

Randall B. Neff and Lynn Tilman "An Example of HP-25 Programming" Hewlett-Packard Journal: November 1975.  pg. 6
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.