Post Reply 
Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
07-09-2019, 01:06 AM (This post was last modified: 07-09-2019 07:47 AM by Namir.)
Post: #7
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE"
Quote:Question: Have you insured in the algorithm that the indefinite loop exit condition is guaranteed to occur? In other words, is it possible that some inputs might cause the "delta" to always be greater than the tolerance value?

Good point. Here is the HP-71B BASIC listing using a new variable M to control the maximum number of iterations:
Code:

10 REM SOLVER FOR ANY VARIABLE
20 DIM X(20)
30 READ N, K, T, M
40 DATA 4, 1, 1E-7, 50
50 FOR I=1 TO N
60 IF I=K THE DISP "GUESS FOR ";
70 DISP "X(";I;")"; @ INPUT X(I)
80 NEXT I
90 H = 0.01*(1 + ABS(X(K)))
100 GOSUB 1000 @ F0 = F
110 X0=X(K) @ X(K) = X0 + H
120 GOSUB 1000
130 D = H * F0 /(F - F0)
140 X(K) = X0 - D
145 M = M - 1
150 IF ABS(D) > T AND M > 0 THEN 90
160 DISP "ROOT = ";X(K)
1780 END
1000 REM CALCULATE F(X)=0
1001 REM X(1) = FV = PV*(1+I)^N
1002 REM X(2) = PV
1003 REM X(3) = I
1004 REM X(4) N
1010 F = X(1)-X(2)*(1+X(3))^X(4)
1020 RETURN

Here is the update memory map for the HP-41C to include R28 to store the maximum number of iterations.

Code:
R00 = I
R01 = X(1)
R02 = X(2)
....
R20 = X(20)
R21 = N
R22 = K
R23 = Toler
R24 = h
R25 = F0
R26 = D
R27 = X0
R28 = MAX_ITERS

And here is the HP-41C listing that uses register R28 to control the maximum number of iterations:

Code:
LBL "MVSOLV"
LBL A
"N?"
PROMPT
STO 21
1E3
/
1
+
STO 00
"K?"
PROMPT
STO 22
"TOLER?"
PROMPT
STO 23
"MAXITERS?"
PROMPT
STO 28
LBL 00        # Input variables
RCL 22
RCL 00
INT
CLA
X#Y?
"GUESS "
|-"X<"
FIX 0
ARCL X
|-">?"
FIX 5
PROMPT
STO IND 00
ISG 00
GTO 00        # end of input loop
LBL 01
RCL IND 22
ABS
1
STO- 28        # MAX_ITRER = MAX_ITET - 1
+
0.01
*
STO 24        # calculate and store h
XEQ E
STO 25        # calculate F0
RCL IND 22
STO 27        # X0 = X(K)
RCL 24
STO+ IND 22    # X(K) = X(K) + h
XEQ E
RCL 25
-
1/X
RCL 25
*
RCL 24
*
STO 26        # Diff = X(K) - f(X(K)) / f'(X(K))
RCL 27
-
CHS
STO IND 22    # X(K) = X(K) - Diff
RCL 26
ABS
RCL 23
RCL 28
X=0?
GTO 02
RDN
X<Y?        # Tolerance less than absolute difference in X(K)?
GTO 01        # resume iterations
LBL 02
"ROOT="
RCL IND 22
ARCL X
PROMPT
RTN
LBL E
1
RCL 03
+
RCL 04
Y^X
RCL 02
*
RCL 01
-
RTN

Can you plese update the HP48GX (and also the HP-50G) listing to use the maximum number of iterations of 55?????

with many thanks!

Namir
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Need help with RPL Listing for "SOLVER FOR ANY VARIABLE" - Namir - 07-09-2019 01:06 AM



User(s) browsing this thread: 1 Guest(s)