Request TVM formula guidance
|
12-11-2014, 10:02 AM
Post: #5
|
|||
|
|||
RE: Request TVM formula guidance
(12-03-2014 09:17 PM)Dieter Wrote: [quote='iMatt04bit' pid='22217' dateline='1417498436'] Quote:Even with very small values of i ?-) It works with an interest value entry of 0.005/12, and up to 999/12. The very small value is not solving as well as I want, though still accurate, but normal values do solve really well. I still get results that match the hp12c and hp20b for all values, so I feel okay with the code so far. It should solve to the maximum of display characters as input - still have to test it out to see how well that works. I still have more scenarios to run through the code to be certain that it is covering all the conditions. You should see the spreadsheet that I have with all the TVM scenarios and much of where I worked out the i guesser code formulas. Quote:Some HP calculators use special functions like ln(1+x) or exp(x)-1 to ensure exact results even under these circumstances. Have you implemented something similar in Java? What's the working precision? Standard double precision, i.e. approx. 15 decimal digits? The precision is exact in all tested cases to the hp12c emulator I am using, and 1e-10 almost always vs the hp20b emulator, except a rare occasional 1e-9 difference. Java seems to be producing 1e-15 precision, but I really don't have a method to test it fully. I do have logarithms, Eulers, all kinds of good intrinsic math formulas in Java. I am very interested in applying the two functions you mention. (12-02-2014 05:33 AM)iMatt04bit Wrote: I just need to know if I have a correct derivative. ...if only I had taken Calculus... Quote:If you didn't there is always Wolfram Alpha. ;-) I checked that out (thanks!), its really nice site. I also found one that solves and gives a tutorial at calculus-calculator.com (12-02-2014 05:33 AM)iMatt04bit Wrote: // formula from the hp12c manual, solves to 0 for matched I converted to R, used below Quote:I assume you mean f(R), not f(x). Yes, as in R being the mutating value of i/100, fed into the formulas for fx and fx' (12-02-2014 05:33 AM)iMatt04bit Wrote: // the derivative as best I know Hmmm... not quite. ;-) I found the real one and its working well. Its really odd how that version and similar others were producing reasonable results. At times I think it had better results than the real thing. But it seemed to be confined to particular ranges and I wanted more. (12-02-2014 05:33 AM)iMatt04bit Wrote: // compute for +I Quote:Could you explain this a bit more in detail? I have since moved to just using one of those formulas. I was having a bad sign day... These functions are inside a loop to form the complete function of the Newton method. The fx function is the HP TVM formula that solves to zero, is in a variable named fxA. The fx' function which has this massively long derivative formula, is in a variable named fxB. Both functions receive the input of R, which at first is the seed value (aka guess of i), in the form R = x/100, where x is the i value in the loop. Next these function results are computed into the evolving version of i, in variable x. So next: x = x - fxA/fxB; When the loop cycles again the x value feeds into R, which in turn feeds into fxA,fxB. As in: R = x/100; I really hope this is the right way, it seemed so from examples I have reviewed. I still think it takes far too many iterations to solve. The partial derivatives that I was using solved much faster. But still the same format of x = x - fxA/fxB was in use. Do you know how fast this should solve? A rough estimate of expected iterations? For an initial value/guess of i, there is a set of conditional code statements which calculates an estimate depending on variations of the TVM inputs of PV,PMT,FV and if all are non-zero, or if one of them is zero, and also per signs. The estimates are within 0.5 of the solution at times, and aimed slightly above the expected value of i. I also use a rough estimater that simply walks up the powers of 10 from 1 to 9e+10 and tests for a good match using the formula assigned to variable fxA. This involves multiple stepped loops which do not iterate through every digit until refining the value. I am solving both pos/neg double roots - found an example in the WP34S archives. I look for a condition where a double root is expected, like (PV > 0.0) && (PMT < 0.0) && (FV > 0.0), and then I compare multiple guess values, where if there are different signs, then both pos/neg values are queued for the Newton solver. It also catches conditions where a lower value of i will also solve. So both types of roots are covered. When no solution is found, a value of zero is returned. Best idea for an error code that I have at the moment since i != 0 is expected. (12-02-2014 05:33 AM)iMatt04bit Wrote: // breakpoint on difference of prior computed I and current I is <= 5e-12 for fractional I | I < 1 percent Quote:Sorry, I do not quite understand what you want to say here. I save the prior value of computed interest, payment, and the result of the TVM formula in fxA. The differences of these values are compared on each iteration. When little or no difference it is time to break out of the loop. I print every iteration to get a visual on how well the solver is working. The values adf, idf, pdf : are differences of prior and current values. adf : prior fxA and current fxA idf : prior/cur interest pdf : prior/cur pmt dfPMT : computed pmt vs actual PMT x : permutating interest fxA : TVM formula which should resolve to 0.00 under ideal conditions ct : certain near exit conditions engage a counter as a backup loop exit method Code:
Quote:The usual exit condition for the iteration loop is a relative (!) error near the working precision, i.e. here 1E-15. Due to numeric pitfalls this may not occur under some circumstances, so I often use another idea: Since approach Newton's method converges roughly quadratically, the idea is to quit as soon as a relative error as small as, say, 1E-10 is reached. Assuming quadratic convergence, this means that the next iteration would have an error as low as 1E-20 and thus the approximation of i would not change — so you're done and the result is fine. Just an idea... Quote:Finally, here are two links that may be helpful: I have a set of variables where the difference of the prior value and current value is compared. This is done for computed values of pmt, i, fxA, and a difference of pmt vs PMT, where pmt is computed in the loop using the permutating i, and PMT is the TVM value received by the solver function. I have been able to use fairly extreme cutoffs of 1e-15 for many, but have some backup solutions for where they may fail. Still testing things, but I can break out on the solve of 0.005/12, so they seem to be ok. Quote:First you may take a look at the way the TVM equation is solved in the HP41 standard application pac. The code can be found in the HP41 software section. Hint: R07 holds 1+i%/100 and R09 stores i%/100. I'll have to look at that to see how it is used, it looks like good reading. Quote:And finally, as you might have expected, the TVM issue has been discussed numerous time before. Maybe a look at this thread in the old forum may provide some useful information. I think I have been everywhere and now have a nice collection of documents and manuals. Did a lot of reading online about algorithms for solving for root. There is not enough info regarding 5th degree polynomials though. ; ( Quote:Dieter Thanks for the info, much appreciated, Matt |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)