Formula Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Formula Help

Post by JakeJ »

I am using a php class you may be familiar with called financial_class.php. It imitates the financial functions of Excel and there is an extensive list of functions.

The problem I'm having is that the RATE() function is not returning the correct number. I'm getting a much smaller number than Excel returns.

I'm not really in the position to recreate one on my own so I'm wondering if anyone has written a function that returns RATE() as found in Excel. It solves for an interest rate needed based on a period of time, periodic payments, current balance and optionally (an option I need) a target amount.

All help with this is appreciated.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Formula Help

Post by tr0gd0rr »

I had to do some equations just recently. I wish I would have found financial_class.php!

Here is my code: http://sandbox.kendsnyder.com/rate/. It may not be perfect; compare against Excel RATE results.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Formula Help

Post by JakeJ »

I'll pop it in to my code tonight and let you know how it goes.

Thanks!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Formula Help

Post by JakeJ »

Sorry to say but your solution didn't work for me since I needed to calculate a target interest rate based on a future amount.

I came up with a fairly quick solution to it though.

Using the FV() function in the financial class I mentioned before, I was able to loop through a series of FV()'s until the number approximated my target amount. Here's the code.

Code: Select all

//Calculates the interest rate required to reach a certain goal.
//Rate is multiplied by 12 for annualizing
//$payment and $pv should be negative numbers
Function retirementrate($nper, $payment, $pv, $target) {
$rate = .001;
$x = 0;
While ($x <= $target) {
	$x = FV($rate,$nper,$payment,$pv);
	If ($x <= $target * .97) {
		$rate = $rate + .00001;
	} 
	Else {
		$rate = $rate +.000000001;
	}
	If ($rate >= 1) { break; }
}

$rate = $rate * 12;
Return $rate;
}
Post Reply