1
   

Exponential Decay?

 
 
NeoGuin
 
Reply Wed 14 Sep, 2005 07:53 am
This may be more appropiate in the Computer Programming forum but. . .

A book I'm working on has me generating a table for an equation.

I decided to do the old "Exponential Growth/Decay" formula

N=N(0)*e^(k*t)

Where e = natural log
k= rate of growth or decay
t= time.

I wrote a C++ function:
Code:
double calculateExp(double timeZero, int duration, double rate){
//Sub variable
double growthFactor;

//determine growthFactor
growthFactor = rate * duration;
//Complete the equation
return timeZero*(exp(growthFactor));
}


Here's what I came up as a problem:

Starting with 1000 bacteria, with a rate of decay of .112 how many bacteria are left at the end of each hour.

So my inputs were:

Initial Value=1000
rate =-.112 (decay)
Time = 10.

The results were:
Quote:

1 hour = 894
2 '' = 799
3 '' = 714
4 '' = 638
5 '' = 570
6 '' = 509
7 '' = 455
8 '' = 406
9 '' = 362
10 = 323


The code that generated this table was
Code:
for (int length = 1; length <= time ; length++){
cout << "Time: ";
result = calculateExp(value, 1, rateOfChange);
value = (int)result;
cout << value << endl;
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 1 • Views: 2,998 • Replies: 4
No top replies

 
vinsan
 
  1  
Reply Thu 15 Sep, 2005 02:51 am
Whats the problem?
Hey,

What is the problem? The method seems correct.

But the ROUNDING of double result to an integer value may loose precision

Try this java code similar to your C++ for() loop but without loss of precision

Code:for (int length = 1; length <= time ; length++) {
result = calculateExp(1000, length, rateOfChange);
value = (int)result;
System.out.println("Hour "+length+":"+value);
}


This gives following output

Quote:
Hour 1:894
Hour 2:799
Hour 3:714
Hour 4:638
Hour 5:571
Hour 6:510
Hour 7:456
Hour 8:408
Hour 9:364
Hour 10:326


In the above output, Bold results show difference with your output series.

It is due to LOSS of double precision.
0 Replies
 
NeoGuin
 
  1  
Reply Thu 15 Sep, 2005 06:01 am
Basically have the variable 'value' be a double?
0 Replies
 
vinsan
 
  1  
Reply Thu 15 Sep, 2005 06:42 am
Answer
NeoGuin wrote:
Basically have the variable 'value' be a double?


Ok I mean you have my way of calling calculateExp() or you can pass value as double in you method

But then (int)result is required becoz number of bacteria is in whole number.

Smile
0 Replies
 
NeoGuin
 
  1  
Reply Fri 16 Sep, 2005 09:06 am
OK.

But other than loss precision the ROUTINES are correct!
0 Replies
 
 

Related Topics

Evolution 101 - Discussion by gungasnake
Typing Equations on a PC - Discussion by Brandon9000
The Future of Artificial Intelligence - Discussion by Brandon9000
The well known Mind vs Brain. - Discussion by crayon851
Scientists Offer Proof of 'Dark Matter' - Discussion by oralloy
Blue Saturn - Discussion by oralloy
Bald Eagle-DDT Myth Still Flying High - Discussion by gungasnake
DDT: A Weapon of Mass Survival - Discussion by gungasnake
 
  1. Forums
  2. » Exponential Decay?
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.04 seconds on 05/05/2024 at 09:59:13