Post Reply 
PC-1211, PC-1250, etc. TVM
06-02-2024, 09:46 PM (This post was last modified: 06-04-2024 02:11 AM by robve.)
Post: #13
RE: PC-1211, PC-1250, etc. TVM
(06-01-2024 10:13 PM)Albert Chan Wrote:  Plus42 uses NPMT formula and Newton's method for rate too.

Guess is picked so that each iteration, f always get better (closer to zero)
Termination criteria is that if f=0 or it change sign, or f not getting better.

Since ULP's are finite, even if f is at plateau, it always terminate.

Indeed, that is an interesting approach. We could improve this a bit by iterating one more time after detecting a sign change or when f does not get better. We could also compare f to f0 to use the best rate corresponding to f or f0 whichever is closer to zero.

In the Sharp code this would be:
Code:
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 : N=LN((M*K/J-F)/(M*K/J+P))/LN(1+J)
13 PRINT "N=";N : END
14 "J" AREAD I : IF C GOSUB 30
15 PRINT "I%=";I : END
16 "V" AREAD P : IF C GOSUB 39 : P=K*M*(S-1)/J-F*S
17 PRINT "PV=";P : END
18 "M" AREAD M : IF C GOSUB 39 : M=(F*S+P)*J/K/(S-1)
19 PRINT "PMT=";M : END
20 "F" AREAD F : IF C GOSUB 39 : F=(K*M*(S-1)/J-P)/S
21 PRINT "FV=";F : END
' solve Y=NPMT=0 for rate I
30 IF I=0 LET I=.01
31 GOSUB 38 : IF Y=0 RETURN
32 G=1,V=I,I=I+.01 : GOSUB 38
33 T=I,I=I-(I-V)*Y/(Y-W),V=T : GOSUB 38
34 IF SGN Y=SGN W IF ABS Y<ABS W GOTO 33
35 IF Y<>0 IF W<>0 IF G LET G=0 GOTO 33
36 IF ABS Y>ABS W LET I=V
37 RETURN
38 W=Y : GOSUB 39 : Y=(P+F*S)*J/(1-S)+K*M : RETURN
' compute
39 J=.01*I,S=(1+J)^-N,K=1+J*B,C=0 : RETURN

where Y and W are f and f0, respectively.

With this, a more accurate rate is obtain such as for PMT=-40 for the example. For PMT=-400 the rate is 80% and we get very close with 80.00000002%.

I've also changed the code to substitute U=(1-S)/J inline, because when entering the rate I%=80 to compute FV we get a very bad FV=0 instead of FV=-1000 because of catastrophic cancellation in U=(1-S)/J. With the change we get FV=-893.8463964 instead of -1000. As we all know, this catastrophic cancellation can be avoided if we use LN1(x) and EXP1(x), which aren't built-in on SHARPs so we should roll out our own series computations, i.e. the alternative part that Dave has in his code computes LN1 but EXP1 is also required. This is a nice example why this matters (a lot).

- Rob

EDIT: correct typo

"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: 7 Guest(s)