41C/CV root finders
|
05-29-2015, 06:15 PM
(This post was last modified: 05-29-2015 06:27 PM by Dieter.)
Post: #41
|
|||
|
|||
RE: 41C/CV root finders
(05-29-2015 08:04 AM)Ángel Martin Wrote: The formulas are posted below, but this is certainly strange - I need to revise the implementation, it might be something goofed up in the way I program them... That's probably true. See below. (05-29-2015 08:04 AM)Ángel Martin Wrote: f(i) = PV + (1+ip) PMT [ (1- (1+i)^-n) / i] + FV (1+i)^-n The formulas look a bit complicated, and I hope they are implemented efficiently, but anyway: they seem to be correct. So I tried Newton's method based on your formulas, and now look what happens even with a less-than-optimal initial guess like the one you posted. The calculations were done in Excel, so the results may slighty differ, but the essential point should be clear: Code: # i f(i) f'(i) f(i)/f'(i) new i With the estimate I suggested things look like this: Code: # i f(i) f'(i) f(i)/f'(i) new i And here are the results for BEGIN mode with the better estimate: Code: # i f(i) f'(i) f(i)/f'(i) new i So in all cases 4 – 6 iterations will do, depending on the initial estimate. That's why I wonder why 7 to 13 iterations are required before the solver in the TVM ROM converges. This sounds like an error in the implementation. Maybe you can compare the results of every iteration step with the ones in the tables above. By the way, here are the results for an initial estimate of 0,0001: Code: END mode: So in fact just one more iteration is required. (05-29-2015 08:04 AM)Ángel Martin Wrote: PS.- You don't need the Library#4 for the TVM$ module - althought it doesn't hurt to have it always configured on V41 of course. You'll need it for the SandMath, SandMatrix and 41Z if you decide to venture there. OK, good to know. I now am looking a bit into the functions of the Sandmath ROM. Dieter |
|||
05-29-2015, 06:44 PM
Post: #42
|
|||
|
|||
RE: 41C/CV root finders
(05-29-2015 06:15 PM)Dieter Wrote:(05-29-2015 08:04 AM)Ángel Martin Wrote: f(i) = PV + (1+ip) PMT [ (1- (1+i)^-n) / i] + FV (1+i)^-n I think they are correct and correctly implemented now after I fixed the bug - see previous post. - but you're welcome to improve the method any time ;-) (05-29-2015 06:15 PM)Dieter Wrote: So in all cases 4 – 6 iterations will do, depending on the initial estimate. You may have missed my previous post, or perhaps our posts crossed paths... it is down to 4 or 5 iterations now after I fixed the bug I found in the algorithm. Cheers, 'AM "To live or die by your own sword one must first learn to wield it aptly." |
|||
05-29-2015, 07:19 PM
Post: #43
|
|||
|
|||
RE: 41C/CV root finders
(05-29-2015 06:44 PM)Ángel Martin Wrote: I think they are correct and correctly implemented now after I fixed the bug - see previous post. - but you're welcome to improve the method any time ;-) I am using the TVM and TVM' formulas posted earlier in this thread (cf. post #11 and #13). The implementation uses two intermediate results a and b that appear both in the function and the derivative, so the slow functions like powers and logs are minimized. This way the iteration runs faster and uses less memory. Let's see if there is a similar way to optimize the implementation of the formulas you use. ;-) But, more important: (05-29-2015 06:44 PM)Ángel Martin Wrote: You may have missed my previous post, or perhaps our posts crossed paths... Yes, it took me about 20 minutes to compile all the data, so I started editing my post before I could read your update. #-) (05-29-2015 06:44 PM)Ángel Martin Wrote: ...it is down to 4 or 5 iterations now after I fixed the bug I found in the algorithm. Great – mission accomplished. Now the community waits for an updated TVM 1F. :-) Dieter |
|||
05-29-2015, 08:00 PM
Post: #44
|
|||
|
|||
RE: 41C/CV root finders
(05-29-2015 06:44 PM)Ángel Martin Wrote: ...but you're welcome to improve the method any time ;-) There actually is another potential improvement: PMT=0 currently is not handled separately. At least the "Running..." display appears for several seconds on V41, and "impossible cases" like both PV and FV being positive seem to loop forever. In a FOCAL program I would implement the direct calculation for PMT=0 this way: Code: RCL 03 Sometimes one forgets the most simple solution. ;-) Dieter |
|||
05-30-2015, 12:37 PM
(This post was last modified: 05-30-2015 12:46 PM by Dieter.)
Post: #45
|
|||
|
|||
RE: 41C/CV root finders
(05-29-2015 07:19 PM)I Wrote: Let's see if there is a similar way to optimize the implementation of the formulas you use. ;-) There is a similar method like the one suggested in post #13. The calculation of your f(i) and f'(i) could get implemented this way: Code: Let Afterwards –f(i) is divided by f'(i) and this quotient is the delta_i that is then added (!) to the previous estimate. Now every iteration requires just one ln1+x and one e^x-1, the formulas are shorter and maybe even more accurate. Regarding cases with no solutions and infinite looping: I think there is a better way than a simple loop counter. The value of abs(delta_i) could be saved in each loop and get compared to one from the previous iteration. If it is larger, i.e. the iteration is divergent, the program may exit with an error message. I am currently testing this in a 35s program. Dieter |
|||
05-30-2015, 02:04 PM
(This post was last modified: 05-30-2015 02:05 PM by Ángel Martin.)
Post: #46
|
|||
|
|||
RE: 41C/CV root finders
(05-30-2015 12:37 PM)Dieter Wrote: There is a similar method like the one suggested in post #13. The calculation of your f(i) and f'(i) could get implemented this way: Thanks for the additional suggestions Dieter, indeed you're getting to the end of this and that's appreciated. I tend to use more "linear" expressions in MCODE because they don't require any storage of intermediate values, like "a", and "b" above. Yes I know this saves time when reused in both formulas but the problem with 13-digit values is you run out of scratch registers very quickly. Realize you'll need 4 standard registers to store a/b in 13-digit format, and that for convenience in the recall.store actions the stack is already used up with the TVM data. There's always the ALPHA registers of course, but I'm already using M,N, and O. I don't want to use any data register above R5 either. Let me think about that, I like the approach and sure it's worth a try. (05-30-2015 12:37 PM)Dieter Wrote: Regarding cases with no solutions and infinite looping: I think there is a better way than a simple loop counter. The value of abs(delta_i) could be saved in each loop and get compared to one from the previous iteration. If it is larger, i.e. the iteration is divergent, the program may exit with an error message. I am currently testing this in a 35s program. Completely agree. Typically a diverging process won't recoup itself into a converging one, or will it? 'AM "To live or die by your own sword one must first learn to wield it aptly." |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)