Tuesday, August 31, 2010

PHP Exponential Decay Calculator


I've searched the web for hours looking for any kind of PHP scripts that calculate exponential decay. I was surprised not to find any, so I've written one up quickly. It's not very advanced, but it gets the job done, and it does what I need it for. It includes a simple form to enter values, and displays the result once the form is submitted. I will supply the script here for anyone to take and use. If you modify or add to it, please supply me with the new code and I will update it here for others to use as well. The formula used is:
N(t) = N0e-kt
For more help on exponential decay, I found these links to be useful to me:
How to use - If you know the quantity for a time, this script will solve for k. Enter the initial time t, and the quantity k (for example, .5 for half-life), then enter the initial time and quantity for which you want to solve.

Source Code:
<!-- // exponential decay calculator // Original Author: Mike Burch // Created: 29 July 2010 // Description: A simple form and script to calculate exponential decay. --> <h2>exponential decay calculator</h2> Formula: <b><i>N(t) = N<sub>0</sub>e<sup>-kt</sup></i></b><br> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> <table> <tr><td><b><i>t</i></b> (Initial Time)</td> <td><input type="text" name="ti" value="<?php echo @$_POST['ti']; ?>"></td></tr> <tr><td><b><i>k</i></b> (Quantity)</td> <td><input type="text" name="k" value="<?php echo @$_POST['k']; ?>"></td></tr> <tr><td><b><i>t</i></b> (New Time)</td> <td><input type="text" name="t" value="<?php echo @$_POST['t']; ?>"></td></tr> <tr><td><b><i>N<sub>0</sub></i></b> (Initial Quantity)</td> <td><input type="text" name="N0" value="<?php echo @$_POST['N0']; ?>"></td></tr> <tr><td colspan="2" align="right"><input type=submit name="calc" value="Calculate"> </td></tr> </table> </form> <?php if (isset($_POST['calc'])) { $k = $_POST['k']; $ti = $_POST['ti']; $t = $_POST['t']; $N0 = $_POST['N0']; $k = log($k) / $ti; $k = -$k * $t; $N = pow(2.718281828, -$k) * $N0; echo "Result: <b>$N</b>"; } ?> <p>How to use: If after <i><b>t</b></i> (initial time) there is <i><b>k</b></i>  quantity left, how<br> much will be left after <i><b>t</b></i> (new time), with an initial quantity of  <b><i>N<sub>0</sub></i></b>?</p> 

exponential decay calculator

No comments:

Post a Comment