PC-1211, PC-1250, etc. TVM
|
06-04-2024, 11:35 PM
Post: #19
|
|||
|
|||
RE: PC-1211, PC-1250, etc. TVM
Albert, we're mixing methods and various different code snippets which becomes a bit of a confusing mess. So I was mistaken to believe that a sign change suffices even with one more iteration I had suggested (but "not necessarily", do you mean more than one or none?). This is not right, to just stop after a sign change, at least not for the method based on the earlier post but modified to solve NPMT=0 rather than NPV=0. Counter examples are not hard to find.
Take for example: B=1 PMT=1,000 PV=900 FV=-1,000 N=40 which requires two more iterations to get I%=-50% correct on the SHARP (10 digits BCD). Milage may vary depending on precision and float representations. Another example "how to be a millionaire": B=0 PMT=-1,000 PV=0 FV=1,000,000 N=360 which gives I%/period=0.498036584 but that takes nine more iterations after the sign change to get there. Granted, this is with solving NPMT=0 combined with the original approach to iterate until sign change f and f0 and as long as the rate improves i<i0 which is: 32 G=9,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 Setting G=9 as above allows up to 9 more iterations (or more if i<i0 again) to always terminate. We also want to avoid Y=W that errors out (the slope hits zero). And when the final iteration produces opposite f and f0 signs we can take a weighted average as a final interpolation of the root: 35 IF Y<>0 IF W<>0 IF Y<>W IF G LET G=G-1 : GOTO 33 36 IF SGN Y<>SGN W LET I=(I*ABS W+V*ABS Y)/(ABS Y+ABS W) 37 RETURN This runs fairly efficiently, as far as vintage 80s CMOS 8 bit calculators are concerned i.e. a couple of seconds at most. I know, there are many other ways to implement a solver with Newton, Halley, Ostrowski or others. On the other hand, it is nice to have a small and simple program to get accurate answers while guaranteeing termination. - Rob "I count on old friends to remain rational" |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 15 Guest(s)