41C/CV root finders

05272015, 12:30 AM
Post: #21




RE: 41C/CV root finders
(05262015 10:08 PM)Dieter Wrote: I could do so if I was more familiar with V41. ;) Dieter  V41 is very easy to setup, load Application ROMs (using .mod files) and run apps much faster than a real 41. You can basically access ALL the HP ROMs plus most of the 3rd party ROMs ever released. You can probably download, install and learn it in the time it takes to add one of your always interesting posts. For real number crunching, of course a physical machine is preferred (unless there is a long runtime, then V41 is preferred), but to explore new software, or the occasional "what are those guys talking about, I just gotta know" itch, V41 is likely the easiest way to load a new Application ROM. Recommended!! Bob Prosperi 

05272015, 05:17 AM
(This post was last modified: 05272015 05:22 AM by Ángel Martin.)
Post: #22




RE: 41C/CV root finders
(05262015 10:08 PM)Dieter Wrote:(05262015 09:26 PM)Ángel Martin Wrote: Not sure I follow, I didn't try the example before  but I just ran it an here are the results: Well, the initial guess is i0 = 0,01650475658, alas not greater than 100% (I don't remember saying anything about it being over 100% before, was than in another thread??), and the number of loops (iterations) needed is 7 for the "begin" case and 13 for the "end" case. On the CL (or V41 in turbo mode) it's obtained in the blink of an eye. I'm positive my implementation isn't earthshattering or novelprize material Dieter, I just wanted to have a reliable TVM$ added to the SandMath on the 41 platform  using MCODE. So I wrote it from the scratch, not copied from the 12C or anything. Besides the i% solver it also has a very nice U/I that mimics the 12C design almost to the last detail, with alphanumeric characters for the results. I guess what I'm saying is that it works for me and hopefully also does for other folks. Naturally will be nice to improve it, say implement the counter idea: how many loops are too many and where would you put the cutoff point? Cheers, 'AM 

05272015, 01:50 PM
(This post was last modified: 05272015 01:52 PM by Dieter.)
Post: #23




RE: 41C/CV root finders
(05272015 05:17 AM)Ángel Martin Wrote: Well, the initial guess is i0 = 0,01650475658, alas not greater than 100% (I don't remember saying anything about it being over 100% before, was than in another thread??) ... No, that was me. ;) You said the inital guess was evaluated as abs(PV+n*PMT+FV))^(1/n). Which is almost always > 1 i.e. > 100%. For the example the result is 150^0,1 = 1,650475 = 165,0475%. Hmmm... the actual estimate is exactly 1/100 of this, so your i0 formula must be slightly different. (05272015 05:17 AM)Ángel Martin Wrote: ...and the number of loops (iterations) needed is 7 for the "begin" case and 13 for the "end" case. On the CL (or V41 in turbo mode) it's obtained in the blink of an eye. Hmm... 7 or 13 iterations? I am sure it works for you, but maybe... maybe I can trigger your ambition a bit anyway...? ;) I tried a simple program in HP41 user code. It returns the two interest rates in both cases (begin and end mode) after merely 4 iterations. And both results are dead on. I also tried the same algorithm with your initial estimate. Again, it took not more than 5 iterations, and both results were exact. (05272015 05:17 AM)Ángel Martin Wrote: I guess what I'm saying is that it works for me and hopefully also does for other folks. Naturally will be nice to improve it, say implement the counter idea: how many loops are too many and where would you put the cutoff point? Please don't get me wrong. Everything is fine if the current implementation works for you. But I can't help: my ambition is to trigger yours. ;) Dieter 

05272015, 03:03 PM
Post: #24




RE: 41C/CV root finders
(05272015 01:50 PM)Dieter Wrote: You said the inital guess was evaluated as abs(PV+n*PMT+FV))^(1/n). Which is almost always > 1 i.e. > 100%. For the example the result is 150^0,1 = 1,650475 = 165,0475%. Hmmm... the actual estimate is exactly 1/100 of this, so your i0 formula must be slightly different. correct  it is divided by 100 ;) (05272015 01:50 PM)Dieter Wrote: Hmm... 7 or 13 iterations? I am sure it works for you, but maybe... maybe I can trigger your ambition a bit anyway...? ;) I tried a simple program in HP41 user code. It returns the two interest rates in both cases (begin and end mode) after merely 4 iterations. And both results are dead on. We're comparing apples to oranges  the number of MCODE loops are not exactly the same concepts as the iterations in the FOCAL code, sorry but that's not a onetoone match. Besides the MCODE speed is significantly faster... are you saying that your FOCAL program finds the solution *faster* than the 12C? (05272015 01:50 PM)Dieter Wrote: Please don't get me wrong. Everything is fine if the current implementation works for you. But I can't help: my ambition is to trigger yours. ;) And you have achieved that, so which initial guess do you suggest I should try? Cheers, ÁM 

05272015, 07:15 PM
(This post was last modified: 05272015 07:42 PM by Dieter.)
Post: #25




RE: 41C/CV root finders
(05272015 03:03 PM)Ángel Martin Wrote: correct  it is divided by 100 ;) I see. So the formula is abs(pv+n*pmt+fv)^(1/n) / 100. I tried this estimate for a number of different scenarios, and in most cases the result is somewhere between 1 and 2%. So one could just as well use a fixed estimate of 1%. Or did I get something wrong? (05272015 03:03 PM)Ángel Martin Wrote: We're comparing apples to oranges  the number of MCODE loops are not exactly the same concepts as the iterations in the FOCAL code, sorry but that's not a onetoone match. But why does it require so many iterations? And why is the result off in the last digits, although 13 digit extended precision is used? Sorry, I just want to understand. (05272015 03:03 PM)Ángel Martin Wrote: Besides the MCODE speed is significantly faster... are you saying that your FOCAL program finds the solution *faster* than the 12C? No, of course not. #) Although I do not own a 12C I assume its internal "machine code" runs much faster than my trusted 41C/CV. Here one iteration takes about two seconds, so the result for the test case appears in about 10 seconds. On the 35s it's about 3 seconds. And on the 34s it's virtually instant. (05272015 03:03 PM)Ángel Martin Wrote: And you have achieved that, so which initial guess do you suggest I should try? I would suggest the formula at the bottom of post #13 in this thread. There you'll also find the other mathematics I use. However, cases may exist where the denominator of i0 becomes zero. So I would do it this way: Code: i0 = 2 / n * (PV + n*PMT + FV) / (PMT * (n±1) + 2*PV) Code: i0 = 2 / (n±1) In both cases use n–1 for END mode and n+1 for BEGIN. BTW, if the nominator is zero, i.e. i0 itself is zero, the program can exit: in this case the interest rate actually is zero. Exiting at this point also avoids the usual problems that arise when the TVM equation is evaluated at i=0. For the record, this is how i converges in the test case, using the suggested estimate and a simple FOCAL program with 10digit precision: Code: # end mode begin mode Of course you may also simply start at i0 = 10^{–4}. The subsequent approximations then are essentially the same as listed above. ;) Dieter 

05272015, 10:09 PM
Post: #26




RE: 41C/CV root finders
(05212015 07:36 PM)Dieter Wrote: Try the standard example: Hi, Just for fun, I tried out the "standard example" above on my financial calculators, HP37E and 38C. Also tried on an emulator for the 12C. On each of these, if I tried to solve for "i" the display would show "Error 5" apparently for "illegal cash flows." However, if I plug in the interest rates you got, and solved for n, PV. PMT & FV, I get exactly the other values. My HP41CV is not working, so I couldn't try out the interest rate solver in the finance pac or in the PPC ROM. I'm curious, has anyone tried out this example on either the Finance Pac or PPC ROM TVM programs on the HP41 ? Does it yield the results shown above or does it give an error message? Bob 

05272015, 11:08 PM
Post: #27




RE: 41C/CV root finders
(05272015 12:30 AM)rprosperi Wrote: Dieter  V41 is very easy to setup, load Application ROMs (using .mod files) It's not very intuitive. But OK, I now played around a bit with it, I read the help file and then I remembered how to access the various ROM modules. But no, I do not have a .mod file of Ángel's TVM module. I fear I would not even know where to look for it. #) (05272015 12:30 AM)rprosperi Wrote: ...and run apps much faster than a real 41. That's one of the nontrivial things. I use V41 version 8. There are two speed controls in the menu that is accessed via Options > More settings. I never understood the idea behind the "speed" and the "turbo" setting. The turbo label says "activate with TAB"  and the help file just says this: that this function is activated by the TAB key... #) Yes, V41 is really nice. But it takes its time to get accustomed to it. Dieter 

05272015, 11:16 PM
(This post was last modified: 05272015 11:40 PM by Dieter.)
Post: #28




RE: 41C/CV root finders
(05272015 10:09 PM)bshoring Wrote: Just for fun, I tried out the "standard example" above on my financial calculators, HP37E and 38C. Also tried on an emulator for the 12C. On each of these, if I tried to solve for "i" the display would show "Error 5" apparently for "illegal cash flows." However, if I plug in the interest rates you got, and solved for n, PV. PMT & FV, I get exactly the other values. Yes, the example is not trivial. Both PV and FV are positive, so that there are two possible solutions for the interest rate – or possibly no solution at all. ;) (05272015 10:09 PM)bshoring Wrote: My HP41CV is not working, so I couldn't try out the interest rate solver in the finance pac or in the PPC ROM. I just tried V41 with the TVM function of the Advantage Pac. Result: "$ERROR". #) Maybe this is HP's way of telling the user that this TVM scenario is not trivial and may have no or multiple solutions. Or maybe in their opinion it doesn't make much sense financially, although there is a mathematical solution (resp. even two). Dieter 

05282015, 05:44 AM
(This post was last modified: 05282015 07:13 AM by Ángel Martin.)
Post: #29




RE: 41C/CV root finders
(05272015 11:08 PM)Dieter Wrote: But no, I do not have a .mod file of Ángel's TVM module. I fear I would not even know where to look for it. #) It's posted at TOS but I cannot copypaste the link here as we all know. I can send you the module via email, pls. send me your address via PM. 'A. Edit: here's the link to the documentation though: http://hp41.claughan.com/file/TVM_1E%20QRG.pdf 

05282015, 06:19 AM
Post: #30




RE: 41C/CV root finders
(05272015 10:09 PM)bshoring Wrote: I'm curious, has anyone tried out this example on either the Finance Pac or PPC ROM TVM programs on the HP41 ? Does it yield the results shown above or does it give an error message? I just tried the Advantage's TVM and the PPC's FI  both flunk. They are also included in the TVM Module BTW So it's a relief to see how my TVM$ MCODE implementation succeeds, even if using more iterations than the optimal ;) Cheers, 'AM 

05282015, 08:42 AM
(This post was last modified: 05282015 08:42 AM by Dieter.)
Post: #31




RE: 41C/CV root finders
(05282015 06:19 AM)Ángel Martin Wrote: I just tried the Advantage's TVM and the PPC's FI  both flunk. I remember also the Standard Pac includes a TVM program ("FIN"). Tried it in V41, et voilà – there is a solution: "I=14,44%". However, HP else seems to rule out certain combinations of the TVM variables. Maybe these are actually those that lead to multiple or no solution(s). This can be checked. In fact solving such cases (and some others) is not as easy as it is with a continuously increasing or decreasing TVM function. And finding an initial estimate isn't either. For instance, the guess I posted may cause the solver to find a negative root even if there is a positive one. Maybe the best option then is having the user provide an estimate. #) Dieter 

05282015, 09:59 AM
(This post was last modified: 05282015 09:59 AM by Dieter.)
Post: #32




RE: 41C/CV root finders
(05272015 07:15 PM)I Wrote: I would suggest the formula at the bottom of post #13 in this thread. There you'll also find the other mathematics I use. Here's another, very simple method: Code: i0 = 2 / n * (PV + n*PMT + FV) / (FV – PV) The estimate is the same for BEGIN and END mode. Here the denominator only becomes zero if FV = PV, which does not make much sense anyway. ;) This is a simplification of another estimate that I use for quite some time which works very well. The original formula uses (n+1) and (n–1) as the factors at PV resp. FV, while this simplified method uses n for both. But again, there is no "golden estimate" that works well for all possible cases. Dieter 

05282015, 11:27 AM
(This post was last modified: 05282015 11:28 AM by Ángel Martin.)
Post: #33




RE: 41C/CV root finders
(05282015 09:59 AM)Dieter Wrote: Here's another, very simple method: I'll try this one first  easier to fit in the current room available. Will report results when done. I agree if FV = PV the user deserves to get an error message, ha! As to why the accuracy isn't 13digits in the result  I was trying to avoid instabilities in the Newton algorithm, which happen due to oscillations around the result. I probably went overboard since the code checks that the delta be less than 1 E8 and not 1 E10  I just assumed that many decimal digits don't make much sense for the nature of the variable, the interest rate (where anything beyond 2 decimal digits will probably be frowned at). ÁM 

05282015, 01:54 PM
Post: #34




RE: 41C/CV root finders
With the new initial estimation there's a marginal improvement  two iteration less if compared against e8, or just one less if using the e9 criteria.
It occured to me the reason for the larger number of loops could be that I check for f(i)/f '(i) to meet that criteria, and not for f(i) alone. This is more demanding when f '(i)<1, as the quotient would be larger than the numerator and thus a bit counterproductive... 

05282015, 06:33 PM
Post: #35




RE: 41C/CV root finders
(05282015 01:54 PM)Ángel Martin Wrote: With the new initial estimation there's a marginal improvement  two iteration less if compared against e8, or just one less if using the e9 criteria. This sounds like there is something wrong with f(i) and/or f'(i). Could you post these two formulas here? A Newtonstyle solver should converge roughly quadratically, and the version I use does so. Here are the iteration results for the sample case. "err_abs" is the applied correction (delta_i) in that iteration step, and "err_rel" is the quotient of this and the current approximation, i.e. delta_i : i. Code: Initial estimate: 0,08571428572 This nicely shows the quadratic convergence: the error is order of 10^{–1}, 10^{–2}, 10^{–5} and finally 10^{–9}, Here the program quits since 1,72 E9 is below the error limit of 5 E–8. (05282015 01:54 PM)Ángel Martin Wrote: It occured to me the reason for the larger number of loops could be that I check for f(i)/f '(i) to meet that criteria, and not for f(i) alone. Sure, this is what I do as well. f(i)/f'(i) is the correction term, the mentioned "delta i". My 10digit program quits if delta_i : i drops below 5E–8. Due to the quadratic convergence one can expect the result to be exact to 12–14 digits, plus/minus a slight unavoidable error in the last digit(s) due to roundoff. (05282015 01:54 PM)Ángel Martin Wrote: This is more demanding when f '(i)<1, as the quotient would be larger than the numerator and thus a bit counterproductive... Are there really cases where f'(i) is less than 1? Usually f'(i) is of similar magnitude as the payments, i.e. around 100 or 1000. For the sample case I get values around 4000 or 5000. You seem to use a different definition of the TVM function and its derivative. Sounds interesting, so again I would like to ask for the formulas you use. BTW, thank you for the MOD files. I will use them with V41 and see what's in there. ;) Dieter 

05282015, 09:37 PM
(This post was last modified: 05282015 09:38 PM by bshoring.)
Post: #36




RE: 41C/CV root finders
(05282015 06:19 AM)Ángel Martin Wrote:(05272015 10:09 PM)bshoring Wrote: I'm curious, has anyone tried out this example on either the Finance Pac or PPC ROM TVM programs on the HP41 ? Does it yield the results shown above or does it give an error message? For what it's worth, I played around with a few scenarios where the PV & FV are of the same sign and the PMT is of the opposite. When set to Begin Mode, it will compute "I" if PV is very small and FV is much larger, say 6 and 390 respectively. If in End mode, the reverse is true. Otherwise I get Error 5. This is on an HP37E. Regards, Bob 

05282015, 09:50 PM
Post: #37




RE: 41C/CV root finders
(05282015 05:44 AM)Ángel Martin Wrote: [ TVM module ] Got it (thank you), and installed it on V41 version 8. Here are the results:
I admit I would not have expected times near 10 or 20 seconds on V41. This would translate to about 15...30 seconds on a physical '41. Do you think there is something wrong with my V41 setup? Dieter 

05292015, 07:58 AM
Post: #38




RE: 41C/CV root finders
(05282015 09:50 PM)Dieter Wrote: [*] After turning on V41 the autoassignment starts ("Working..."). But the assigned functions are not those of the TVM module. Function names like BITVIEW or ROTXY seem to refer to a different ROM. I had the Advantage ROM installed. After removing this, everything works fine. Now only TVM 1E and the P#4 library are installed. You had an XROM conflict because the TVM uses the same number as one of the Advantage pages. Simply removing the advantage solves it, as you did. As I said before, the TVM$ Module also has a copy of the Advantage's TVM so you don;t need to have both plugged in. Ditto for the PPC's "FI" and 'FIN" programs  they're also included in the TVM$ Module. (05282015 09:50 PM)Dieter Wrote: [*] For this result the I$ function required about 9 seconds on V41. This is about the same as on my hardware 41CV with a simple FOCAL program. I am using V41 version 8 with default speed settings, which runs faster than my hardware 41CV. However, I noticed that after 4 seconds the "Running..." display flickered a few times. The LCD flickering is intended  it does it each time a new iteration begins. (05282015 09:50 PM)Dieter Wrote: [*] I tried the same example in BEGIN mode. This time the I$ function required 19 seconds on V41, and the result was correct when rounded to 8 significant digits: "I=10,20509781". The speed depends on the selected settings, I imagine you have already seen that in V41 configuration screens. You can always press the TAB key when the program runs to engage the TURBO mode  regardless of the settings  while you KEEP it pressed. More on the thread below... 

05292015, 08:04 AM
(This post was last modified: 05292015 08:16 AM by Ángel Martin.)
Post: #39




RE: 41C/CV root finders
(05282015 06:33 PM)Dieter Wrote: This sounds like there is something wrong with f(i) and/or f'(i). Could you post these two formulas here? (05282015 06:33 PM)Dieter Wrote: ..... f(i)/f'(i) is the correction term, the mentioned "delta i". My 10digit program quits if delta_i : i drops below 5E–8. Not really, I approach this from a math standpoint and not a financial expert  which I'm not by any stretch of the imagination. 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... the execution times were comparable to the 12C in the examples I tried so I assumed it was ok though. f(i) as per the Advantage's manual: f(i) = PV + (1+ip) PMT [ (1 (1+i)^n) / i] + FV (1+i)^n Hence: f ’ (i) = (PMT / i^2 ) * [ (1+i)^(n)  1 ] + n * [PMT (1 + ip)/i – FV ] * (1+i)^(n+1) where p=1 in Begin mode or 0 in End mode. BTW the Source Code is also posted at TOS  so anyone with an inclination can have a look and help me find the glitch  it there's one ;) Çheers, ÁM 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. 

05292015, 05:54 PM
(This post was last modified: 05292015 05:56 PM by Ángel Martin.)
Post: #40




RE: 41C/CV root finders
(05292015 08:04 AM)Ángel Martin Wrote: ... but this is certainly strange  I need to revise the implementation, it might be something goofed up in the way I program them... the execution times were comparable to the 12C in the examples I tried so I assumed it was ok though. Found a bug in the code that was the cause the number of iterations needed go beyond the needed ones, so now (after correction) both Begin and End cases require 6 iterations using a 1E9 delta error, or just 4/5 using the 1E8 criteria. So I think this must have been the issue, thanks for your pointers and instigating support ;) Cheers, 'AM 

« Next Oldest  Next Newest »

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