HP Forums
Looking for TVM formulas - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not remotely HP Calculators (/forum-9.html)
+--- Thread: Looking for TVM formulas (/thread-1012.html)

Pages: 1 2


RE: Looking for TVM formulas - Thomas Klemm - 04-04-2014 05:23 PM

(04-04-2014 02:07 PM)Dave Britten Wrote:  Awesome, I used Thomas' suggestion for calculating (1 + i%) ^ n - 1 using the more accurate e^x-1 formula, 2 * sinh(x / 2) * e ^ (x / 2), and now for the Susan example, my TI-95 program is giving me FV = -333,783.3499486.

I've used that in Jeff Kearns' program for the HP-15C:
Code:
001 - 42,21,15  LBL E        
002 -    44 24  STO (i)      
003 -    45  2  RCL 2        
004 -       26  EEX          
005 -        2  2            
006 -       10  /            
007 -       36  ENTER        
008 -       36  ENTER        
009 -        1  1            
010 -       40  +            
011 -    43 12  LN           
012 -       34  x<>y         
013 -    43 36  LSTx         
014 -        1  1            
015 - 43,30, 6  TEST 6       
016 -       30  -            
017 -       10  /            
018 -       20  x            
019 - 45,20, 1  RCLx 1       
020 -       36  ENTER        
021 -       12  e^x          
022 - 45,20, 3  RCLx 3       
023 -       34  x<>y         
024 -        2  2            
025 -       10  /            
026 - 42,22,23  SINH         
027 -    43 36  LSTx         
028 -       12  e^x          
029 -       20  x            
030 -        2  2            
031 -       20  x            
032 - 45,20, 4  RCLx 4       
033 -       26  EEX          
034 -        2  2            
035 - 45,10, 2  RCL/ 2       
036 - 45,40, 6  RCL+ 6       
037 -       20  x            
038 -       40  +            
039 - 45,40, 5  RCL+ 5       
040 -    43 32  RTN

Now I get 333,783.3500 instead of 333,783.3508.

Cheers
Thomas


RE: Looking for TVM formulas - Jeff_Kearns - 04-04-2014 09:44 PM

(04-04-2014 02:07 PM)Dave Britten Wrote:  Program size is currently 478 steps, and I figure there will be another 150-200 when all that is implemented.

Wow! That is quite the program... Just FYI, the total program length (including Solver) for the HP-29C or HP-11C is 71 steps in length. Granted, it does poorly (if at all) at solving for interest. The program size for calculators with built-in solvers is substantially smaller (and finds solutions for periodic interest rates). What does your program do exactly? I thought you wanted a TVM solver.

Regards,

Jeff


RE: Looking for TVM formulas - Dave Britten - 04-04-2014 10:12 PM

(04-04-2014 09:44 PM)Jeff_Kearns Wrote:  
(04-04-2014 02:07 PM)Dave Britten Wrote:  Program size is currently 478 steps, and I figure there will be another 150-200 when all that is implemented.

Wow! That is quite the program... Just FYI, the total program length (including Solver) for the HP-29C or HP-11C is 71 steps in length. Granted, it does poorly (if at all) at solving for interest. The program size for calculators with built-in solvers is substantially smaller (and finds solutions for periodic interest rates). What does your program do exactly? I thought you wanted a TVM solver.

Regards,

Jeff

TVM solver, mostly. The TI-95 has lots of RAM to work with, for a keystroke programmable calculator (about 8KB), and the default partitioning gives you about 1000 steps/bytes of work RAM to use for coding. Programs can be saved/loaded/executed from the file partition, which defaults to about 5KB (5000 steps or so). There are also instructions to save ranges of registers into the file memory for later retrieval. With default settings, you get 125 storage registers, all addressible directly or indirectly (any register can be used for indirection). I think it's also possible to shrink file space to zero, and rely on an 8/32 KB expansion module for your file storage, but you'd have to be working with some enormous programs to need that.

Off the top of my head, these are the routines in the program so far:

1. Main menu - Allows accessing the TVM menu, or selecting Begin/End modes. Amort will be accessible from here when I implement it.
2. TVM menu - Shows the 5 TVM variables, and allows storing a value into each, or requesting solutions. The 95 allows testing for the INV prefix key being used to modify a soft key, so each of the five soft key handlers checks for it, calls one of five "solve" routines if needed, then puts the display value into the selected variable.
3. Five "solve" routines - Three of these are direct solutions (PV, PMT, FV), and the other two prepare...
4. ...The numeric solver - A straight port of the HP 20S solver, with a couple changes specific to this progra. It chokes on overflows trying to solve for n or i% in the Susan example, so I probably ought to replace it. Works okay for 'typical' problems, but isn't particularly fast, as it's lacking any initial guess logic (just starts at 1).
5. ln(1+x) - Uses numeric methods discussed in this thread.
6. e^x - 1 - Ditto.
7. (1 + i%)^n - Uses the ln(1 + x) routine for greater accuracy. Needed by all of the solution equations.
8. ((1 + i%)^n) - 1 - Also uses e^x - 1, and again needed by the solutions.
9. i% Routine - Returns i% / 100 for calculations. Needs to have some of the repeat calls optimized away.
10. Begin/End Routine - Returns 1 or 0 if Begin or End is selected, respectively.
11. Solver equation - TVM equation with one side solved for zero, for use with numeric solver routine.

So, the (somewhat) redundant direct solutions, menu handlers, math utility routines, and included numeric solver explain the size. Also, the TI needs something like three bytes (steps) for goto, gosub, STO, RCL, comparison tests, and other instructions that require an argument, to allow for the full three or four digits used in the addresses. Any functions prefixed with INV will also use another byte for the prefix.


RE: Looking for TVM formulas - Dieter - 04-05-2014 09:58 PM

(04-04-2014 10:12 PM)Dave Britten Wrote:  3. Five "solve" routines - Three of these are direct solutions (PV, PMT, FV), and the other two prepare...
4. ...The numeric solver - A straight port of the HP 20S solver, with a couple changes specific to this progra. It chokes on overflows trying to solve for n or i% in the Susan example,

Hm, why do you think that there is no direct solution for n and an iterative approach is required?

Consider the basic TVM formula you already posted:

PV * (1 + i)n + PMT * ((1 + i)n – 1)) * ((1 / i) + B) + FV = 0

Then
(1 + i)n – 1 = –(PV + FV) / (PMT * (1 / i + B) + PV)

And thus, using ln1+x for best accuracy:

n = ln1+x(–(PV + FV) / (PMT * (1 / i + B) + PV)) / ln1+x(i)

Or a bit more formal:

\(\large n  =  ln (1 - \frac{PV + FV}{PV + PMT (\frac{1}{i} + B)})  :  ln (1 + i)\)

Try the Susan example:

(1+i)n – 1 = (–(0 + -333783,35)) / ( 0,01 * (1/3,567351598 E-9 + 0) + 0) = 333783,35 / 2803200 = 0,1190722567

ln1+x(0,1190722567) / ln1+x(3,567351598 E-9) = 31536000

Dieter


RE: Looking for TVM formulas - Dave Britten - 04-05-2014 11:38 PM

(04-05-2014 09:58 PM)Dieter Wrote:  Hm, why do you think that there is no direct solution for n and an iterative approach is required?

Oh, I figured there was one, I just hadn't found/derived it yet, so I was feeding it through the numeric solver for now. Thanks for posting that, it should prove useful.


RE: Looking for TVM formulas - Dieter - 04-06-2014 07:46 PM

(04-02-2014 05:18 PM)Thomas Klemm Wrote:  You could use \(e^x-1=2 \cdot sinh(\frac{x}{2}) \cdot e^{\frac{x}{2}}\) instead.

Since we're at it: hyperbolics can also be used for ln1+x.

\(ln(1+x) = 2 \cdot artanh(\frac{x}{x+2})\)

Seems to work well if x is not too large so that the artanh-argument does not get too close to 1.

Dieter


RE: Looking for TVM formulas - Dieter - 04-09-2014 01:36 PM

(04-06-2014 07:46 PM)Dieter Wrote:  Since we're at it: hyperbolics can also be used for ln1+x.

\(ln(1+x) = 2 \cdot artanh(\frac{x}{x+2})\)

Seems to work well if x is not too large so that the artanh-argument does not get too close to 1.

This one seems even better and does not have problems with very large x:

\(ln(1+x) = arsinh(\frac{x^2+2x}{2+2x})\)

For better numeric accuracy this can be written as

\(y = \frac{x}{2};   z = y + \frac{y}{2y+1}\)

\(ln(1+x) = arsinh z\)

With \(z \rightarrow \infty,   arsinh z \rightarrow ln 2z\)  and  \(z \rightarrow \frac{x+1}{2}\),  so that \(arsinh z \rightarrow ln(1+x)\).

Dieter