I'm working on converting a mortgage calculator in PHP, but I don't necessarily need a PHP solution. I'm looking for the logic needed to replicate the Excel RATE
function. I've found a solution which uses bisection, and if worse comes to worse, I use that.
I know someone out there in the interwebs world has knowledge of such a function, so I'd love to have an easy answer instead of creating a solution from scratch.
References:
- http://office.microsoft.com/en-us/excel-help/rate-HP005209232.aspx
- http://en.wikipedia.org/wiki/Newton%27s_method
Thanks
For Laravel use the same function but you remove define
and financial_max_iterations = 20; -> same excel
The code is:
it worked for me
Implementation of the MS Excel RATE() function using the secant method (a finite difference approximation of Newton's method) taken from PHPExcel:
TL;DR: Here's a SQL Server version. It doesn't work for some values, and the PHP code above will probably fail for the same values.
LONG ANSWER: I needed a RATE function for SQL Server. Using the PHPExcel answer above, and using https://charlottecredittechnology.blogspot.com/2013/05/sql-2008-excel-like-rate-function-part.html I wrote a SQL Server scalar function:
Unfortunately it does not always work. Here's the results of some tests I put together and checked using Excel:
Tests (7) and (8) were taken from RATE Function from EXCEL in Swift providing different results and look for the answer using the Newton-Raphson method.
I tried to use the code above, but the results simply aren´t the same as Excel (or Google Spreadsheet).
I dont know if you need to implement this function yet, but in any case, I looked at how this algorithm was built and even though I was not able to access the excel source code (or the google worksheet) I found that this is not a simple calculation. About this math, more can be read here:
https://brownmath.com/bsci/loan.htm#Eq8
The function, in PHP, may be something like this: