Jump to content

Calculating Half Life Decay


 Share

Recommended Posts

This is purely an exercise to exercise the brain, but I just can't seem to wrap my brain around this one. To calculate the ending amounts of half life decay the formula is:

E = Ending Amount

B = Beginning Amount

n = Number of Half Lives

E = B/(2^n)

So if there was 100 units of some material, lets say Hydrogen, in a canister and we wanted to find out how much Hydrogen would be left after 1 half life, then we would do this:

100/(2^1) = 50

After 2 half lives:

100/(2^2) = 25

After 3 half lives:

100/(2^3) = 12.5

But, now let us say that while the original amount of Hydrogen is decaying, we put more Hydrogen into the canister, and now we want to know what the ending amount of total hydrogen per half life that remains in the canister. So let us say that after the first half life occurs that we blow more Hydrogen into the canister (100 units), the results would be:

100/(2^1) = 50 + 100 = 150

After the next half life occurs, we must now calculate both half lives and add them together:

100/(2^2) = 25

+

100/(2^1) = 50

= 75 units of Hydrogen

Lastly, let us say that we continue this pattern it would follow:

100/(2^3) = 12.5

+

100/(2^2) = 25

+

100 = 137.5

Then...

100/(2^4) = 12.5

+

100/(2^3) = 25

+

100/(2^1) = 50

+

100 = 187.5

I would like to be able to display the total amounts of Hydrogen as they decay, with the ability to add in X amount of Hydrogen at any given point, and have the resulting figures 'update'. Meaning, maybe after 1 or 5 or 14 half lives more Hydrogen is added and the resulting total amount can be displayed.

TIA

Starter Code:

$B = InputBox ("Amount of Material", "How much material are you starting With?")
$n = InputBox ("Amount of Half Lives", "How many half lives would you like to calculate for?")


For $iCC=0 To $n
    $E = $B/(2^$iCC)
    ConsoleWrite ("The Amount After " & $iCC & " Half Lives have occured equals: " & $E & @CRLF)
Next
Link to comment
Share on other sites

Well I'm not the one who understand "half life" or whatever, but what you want to do is pretty simple. What I would do is I would create an Array for each Beginning Amount.

Example:

$stuff = _ArrayCreate($B)

Then you can create a function that calculates each element individually and store them back in their location in the Array. And then when you want to find the Total Ending Amount, you can add each element in the array. And if you only want the first Beginning Amount, just pull $stuff[0]; for second Beginning, pull $stuff[1], etc.

But that's just me.

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

I'm too lazy to make this a line graph, or to make the bar graph logarithmic tonight, but this might be a start:

#include <guiconstants.au3>

$B = InputBox ("Amount of Material", "How much material are you starting With?")
$n = InputBox ("Number of Half Lives", "How many half lives would you like to calculate for?")

$hGUI = GUICreate("Half Life", 600, 155)

$Label_InitQty = GUICtrlCreateLabel("Initial quantity = " & $B, 10, 10, 280, 20, $SS_CENTER)
$Label_RemQty = GUICtrlCreateLabel("Remaining quantity = " & $B, 310, 10, 280, 20, $SS_CENTER)
$Progress_Qty = GUICtrlCreateProgress(10, 35, 580, 30, $PBS_SMOOTH)
GUICtrlSetData(-1, 100)

$Slider_HL = GUICtrlCreateSlider(10, 90, 580, 30, $TBS_NOTICKS)
GUICtrlSetLimit(-1, $n, 0)
GUICtrlSetData(-1, 0)
$Label_HL = GUICtrlCreateLabel("Half lives past = 0", 10, 125, 580, 20, $SS_CENTER)
GUISetState()

$HL_Prev = 0
While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    $HL_Slide = GUICtrlRead($Slider_HL)
    If $HL_Slide <> $HL_Prev Then
        $HL_Prev = $HL_Slide
        GUICtrlSetData($Label_HL, "Half lives past = " & $HL_Slide)
        $E = $B / (2 ^ $HL_Slide)
        GUICtrlSetData($Label_RemQty, "Remaining quantity = " & $E)
        GUICtrlSetData($Progress_Qty, $E / $B * 100)
    EndIf
WEnd

:)

P.S. Atomic hydrogen doesn't have a half life, it's stable. Neither does deutrium. You have to go to tritium before you get a half life for a hydrogen isotope, at λ = 12.32yrs.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...