**Disclaimer:
My blog contains content that may be objectionable. Some of the posts in this blog may contain strong language or (because of my religious and personal views) may be offensive to some readers. In the future I may separate my programming posts from personal posts, but for now they are all contained in this blog. I should not have to even mention this, however some readers may be looking at my blog for programming help, therefore I am giving an advanced warning. All posts with material that may be offensive contain 'Objectionable material' at the end of the post title. If you see a post with "Objectionable material" in the title, and you are closed-minded or think that you may get queasy, don't fucking read it.


Thursday, July 29, 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>

Wednesday, July 28, 2010

Wow, it's been forever since I posted anything here. I've been busy working on a project for the last few months, and it seems like there is no end in sight. I had planned for a year before it was complete, so we'll see if I can make it in time. I will try to post here more often, even if just a quick like or two.