Post Reply 
(10C) Mortgage Loan Interest Rate
11-03-2018, 01:51 PM (This post was last modified: 11-05-2018 12:03 AM by Albert Chan.)
Post: #9
RE: (10C) Mortgage Loan Interest Rate
PHP Code:
def nsolve_i(nreps=1e-8verbal=True):
    
1.0/(r*n*n)
    if 
verbal: print 'guess i0 =>\t\t%.15g' i
    
while 1:
        
fi i/(1.-(1.+i)**-n)      # newton's method
        
di = (fi r) / (fi n*fi*(fi-i)/(1.+i)) * i
        
# di = (fi - (fi*r)**0.5) / (fi - n*fi*(fi-i)/(1.+i)) * (i+i)
        
-= di
        
if verbal: print '%+.6e => \t%.15g' % (-dii)
        if 
abs(di) < eps: break
    return 


Tried using Newton's method. It is not as fast, unless f(i) is inlined.
Also, if eps is set too small, i will not converge.
Code:
>>> nsolve_i(n=36, r=86.67/2500)
guess i0 =>             0.0124110212798083
+9.356770e-05 =>        0.0125045889822448
-4.297933e-08 =>        0.012504546002918
-9.079922e-15 =>        0.012504546002909
0.012504546002908958

If newton's method apply to √f(i), i converge faster.
Just replace above di, with the commented version.
Code:
>>> nsolve_i(n=36, r=86.67/2500)  # √f(i) version
guess i0 =>             0.0124110212798083
+9.353061e-05 =>        0.0125045518848863
-5.881977e-09 =>        0.0125045460029088
0.012504546002908835

Correct i = 0x1.99bfbc11210d4P-7 ~ 0.01250454600290888
Although √f(i) version does 1 less iteration, it is more accurate (-26 ULP vs +45 ULP)

Correction: Newton's method is actually faster (with similar error)
I had compared the speed with secant's method using same eps = 1e-8
This is not fair to the Newton version, since the result had a few more good digits.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(10C) Mortgage Loan Interest Rate - Gamo - 10-31-2018, 04:49 AM
RE: (10C) Mortgage Loan Interest Rate - Albert Chan - 11-03-2018 01:51 PM



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