Post Reply 
PC-1211, PC-1250, etc. TVM
06-22-2024, 08:39 PM (This post was last modified: 06-23-2024 08:07 PM by robve.)
Post: #49
RE: PC-1211, PC-1250, etc. TVM
Updated Newton and Hybrid implementations with SHARP PC-1403H TVM test results.

The improved and combined SHARP BASIC TVM program listing includes both parts for the Hybrid and Newton rate solver methods:

Code:
' Switch begin mode on/off: DEF-B
' Enter values: DEF-N or DEF-J (I%) or DEF-V (PV) or DEF-M (PMT) or DEF-F (FV)
' Calculate: press DEF-C (beep) then DEF-N or DEF-J or DEF-V or DEF-M or DEF-F

10 "B" B=B=0 : PRINT "BGN=";MID$("NY",B+1,1) : END
11 "C" C=C=0 : BEEP C : END
12 "N" AREAD N : IF C GOSUB 39 : IF N=0 LET N=LN((K*M-F*J)/(K*M+P*J))/L
13 PRINT "N=";N : END
14 "J" AREAD I : IF C GOSUB 25
15 PRINT "I%=";I : END
16 "V" AREAD P : IF C GOSUB 39 : P=K*M*R/J-F*S
17 PRINT "PV=";P : END
18 "M" AREAD M : IF C GOSUB 39 : M=(P+F*S)*J/K/R
19 PRINT "PMT=";M : END
20 "F" AREAD F : IF C GOSUB 39 : F=(K*M*R/J-P)/S
21 PRINT "FV=";F : END
' check if I%=0
25 G=1,C=0 : IF P+F=-N*M LET I=0 : RETURN
' solve Y=NPMT=0 for rate I% pick initial nonzero best guess and perform newton step
26 V=P+B*M,W=F-B*M
27 Y=(V+W)/N,W=W-V-Y,V=(M+Y)/W,W=(N*N-1)*Y*V/W,I=100*V*(W-3)/(W-1.5)
28 IF ABS I<1E-9 LET I=1E-5*(SGN I+(I=0))
29 IF ABS I>99 LET I=99*SGN I
30 GOSUB 38 : IF Y=0 RETURN

' USE THIS PART FOR HYBRID:
31 V=I,I=I-(K*M-(P+F*S)*J/R)*100/(B*M-(P+F)*(1+N*S/(R+R/J))/R-F)
32 GOSUB 38 : IF Y=0 OR Y=W RETURN
' secant loop
33 T=I,I=I-(I-V)*Y/(Y-W),V=T
34 GOSUB 38 : IF Y<>0 IF ABS Y<ABS W GOTO 33
35 IF Y<>0 IF Y<>W IF G LET G=G-1 : GOTO 33

' USE THIS PART FOR NEWTON:
31 V=I,I=I-(K*M-(P+F*S)*J/R)*100/(B*M-(P+F)*(1+N*S/(R+R/J))/R-F)
34 GOSUB 38 : IF Y<>0 IF ABS Y<ABS W GOTO 31
35 IF Y<>0 IF Y<>W IF G LET G=G-1 : GOTO 31

' pick previous I% after overstepping
36 IF Y<>0 LET I=V
' round very small I% to zero
37 I=(ABS I>1E-9)*I : RETURN
' compute Y=NPMT and save previous Y in W
38 W=Y : GOSUB 40 : Y=K*M-(P+F*S)*J/R : RETURN
' check edge case I%=0 to set J,K,S,R and compute N when requested
39 C=0 : IF I=0 LET J=1,K=1,S=1,R=-N,N=N-(N=0)*(P+F)/(M+(M=0)) : RETURN
' compute L=LN(1+J) and S=(1+J)^-N and R=S-1 using LNP1 and EXPM1 formulas
40 J=.01*I,K=1+J*B,L=1+J,L=LN L-(L-1-J)/L,S=-N*L,R=EXP S,R=R-1-(LN R-S)*R,S=R+1 : RETURN

The Hybrid version executes faster on this machine and its overall accuracy is at least as good as Newton or better (for the set of test cases and other tests that were not included in the results because of similarity).

Add these four lines to compute amortization with <period> DEF-A:

Code:
50 "A" AREAD A : J=.01*I,R=P-F,T=0,V=0
51 FOR K=1 TO N : S=R*J,Q=-M-S,R=R-Q
52 IF K>=A LET T=T+S,V=V+Q : PRINT STR$ K;" ";INT(100*S+.5)/100;"int ";INT(100*Q+.5)/100;"prn"
53 NEXT K : END

- Rob

"I count on old friends to remain rational"
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
PC-1211, PC-1250, etc. TVM - Dave Britten - 03-30-2021, 04:58 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 03-30-2021, 05:07 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 04-01-2021, 05:50 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 05-29-2024, 09:17 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 05-31-2024, 01:11 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-01-2024, 01:07 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-02-2024, 09:46 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-03-2024, 06:44 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-04-2024, 12:26 AM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-04-2024, 11:35 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-05-2024, 07:16 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-05-2024, 10:06 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-06-2024, 11:02 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-08-2024, 06:19 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-08-2024, 09:21 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-09-2024, 12:06 AM
RE: PC-1211, PC-1250, etc. TVM - rprosperi - 06-09-2024, 01:22 PM
RE: PC-1211, PC-1250, etc. TVM - nickapos - 06-12-2024, 05:20 AM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-06-2024, 11:34 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-07-2024, 02:30 AM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-08-2024, 08:38 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-09-2024, 10:12 AM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-11-2024, 07:13 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-11-2024, 10:32 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-12-2024, 12:29 AM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-12-2024, 12:45 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-12-2024, 04:58 PM
RE: PC-1211, PC-1250, etc. TVM - dm319 - 06-22-2024, 12:12 PM
RE: PC-1211, PC-1250, etc. TVM - robve - 06-22-2024 08:39 PM



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