adambeazley Posted March 6, 2009 Share Posted March 6, 2009 Hey guys, I am having trouble figuring out how to do this math without 500 lines of code. Basically i am coding a little calculator that will calculate the average savings and payback that people can expect by installing a radiant barrier(energy efficiency product) in their homes. Here is what I am doing so far: I get their average monthly utility bill and make it a yearly figure (x * 12)- $yearlybill Then i grab the utility inflation rate for their state from my database - $inflrate Now i can calculate the total amount spent on utility bills over 5 years: $cost5 = $yearlybill + $yearlybill * $inflrate + $yearlybill * pow($inflrate,2) + $yearlybill * pow($inflrate,3) + $yearlybill * pow($inflrate,4); ---How can i streamline the above code? (yearly_bill x num%^incremental power) I would like to maybe have a while statement or a function which would return the total savings each year. Im thinking something like: while (sum of ($yearlybill * pow($inflrate, ++i)) < $costofproduct){ start counting then when it is > $cost of product print a result. Sorry just thinking out loud (brain farting). I want to build on that code can basically figure out the amount of time it would take for the savings to pay for the product. In other words, lets say the product is $300 and the average monthly bill is $100. the yearly bill is 1200 for year 1 * 17% savings on energy = $204 in savings for year 1. In year two with 10% inflation the total utility bills would be 1200 * 1.10 = $1320, then with the same 17% savings on energy = $224.40 in savings for year 2, for a total savings of $428.40 over that two year period. Now because the savings are more than the cost of the product, i can return/echo something like - your will pay for this product in energy savings in under 2 years. I hope I am being clear about what i am trying to do. I know how to do this, but its take many lines off code and many if statements and was wondering if there is a more advanced way to do it. Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/148277-solved-inflation-savings-and-payback-calculation/ Share on other sites More sharing options...
sasa Posted March 6, 2009 Share Posted March 6, 2009 <?php $saving_factor = .17;// 17% $inflate_rate = .1;// 10% $product_cost = 428.40; $month_bill = 100; if ($month_bill <= 0 or $saving_factor <= 0) die('never'); if ($inflate_rate != 0) $years = log(($product_cost / $saving_factor / ($month_bill * 12) * $inflate_rate) + 1) / log($inflate_rate + 1); else $years = $product_cost / $saving_factor / ($month_bill * 12); echo 'Your will pay for this product in energy savings in under ', ceil($years), ' year', $years > 1 ? 's' : '', '.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148277-solved-inflation-savings-and-payback-calculation/#findComment-778468 Share on other sites More sharing options...
adambeazley Posted March 7, 2009 Author Share Posted March 7, 2009 wow, thanks that worked great. I really appreciate you taking the time to write this out for me. Ive looked at the log function before, but Im still not really sure how it works. Woudl you mind explaining the math behind the log function? Quote Link to comment https://forums.phpfreaks.com/topic/148277-solved-inflation-savings-and-payback-calculation/#findComment-779189 Share on other sites More sharing options...
sasa Posted March 10, 2009 Share Posted March 10, 2009 let a = mouhth_bill * 12 (year bill) and q = 1 + inflate_rate (inflate factor) then product_cost = a*saving_factor + a*q*saving_factor + a*q^2*saving_factor + ... + a*q^(n-1)*saving_factor (after n years) product_cost = a*saving_factor*(1 + q + q^2 + ... + q^(n-1)) product_cost = a*saving_factor* (q^n - 1)/(q - 1) (look http://en.wikipedia.org/wiki/Geometric_series#Formula) => q^n = product_cost*(q - 1) / (a*saving_factor) + 1 log(q^n) = log(product_cost*(q - 1) / (a*saving_factor) + 1) n* log(q) = log(product_cost*(q - 1) / (a*saving_factor) + 1) n = log(product_cost*(q - 1) / (a*saving_factor) + 1) / log(q) round up n btw q - 1 = inflate_rate (look line 1) Quote Link to comment https://forums.phpfreaks.com/topic/148277-solved-inflation-savings-and-payback-calculation/#findComment-781277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.