Jump to content

Recommended Posts

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.

 

 

 

 

 

<?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' : '', '.';
?>

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?

 

 

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.