Why is My C++ Program for the Bateman Equation Returning NAN?

  • C/C++
  • Thread starter lapo3399
  • Start date
  • Tags
    C++
In summary, The program does not work because the equation is not being properly emulated and the decay constants are not being correctly multiplied.
  • #1
lapo3399
55
0
Hello,

I am attempting to create a program that will evaluate the Bateman equation for radiactive decay series, given several decay constants stored in a text file. I am not sure where this is going wrong, but no matter what chain member I enter in as the desired nuclide to find the mass remaining after 1 million years, it outputs NAN - even when I choose the nuclide that I am certain to have a perceivable value. The equation I am emulating later in the program is shown below, please help to explain why this is not working! All help is appreciated!

http://img515.imageshack.us/img515/5097/screenshotml6.jpg

Code:
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char** argv)
{
	string line; //for file input
	int iter; //stores which nuclide will be calculated for
	
	double dcs[23]; //array for decay constants
	int anum = 1; //counting var
	
	ifstream deconst;
	deconst.open ("decayconstants.txt", ios::in); //file with decay constants
	if (deconst.is_open())
	{
		while (! deconst.eof())
		{
			getline(deconst, line);
			dcs[anum]=strtod(line.c_str(), NULL); //store as double in decay constant array
			anum++;	
		}
		
	}
	
	std::cout << "What nuclide in the chain should be evaluated?";
	std::cin >> iter;
	
        //I separated the equation into multiple parts

	double consts=1; //part one: the multiplied decay constants at the beginning of the equation
	double init = 100; //the intial mass, N(O)
	double time = 1000000; //the variable t, one million years
	double sum=0; // the second half of the equation, the summation
	double totalmass; //the product of consts,init, and sum
		
	for (int i = 1; i < iter; i++)
	{
		consts=consts*dcs[i]; //multiply all the necessary decay constants 
	}
	
	for (int q = 1; q < iter; q++) //the summation
	{
		double expo = exp(-dcs[q]*time); //determine the numerator in the summation
		double denom = 1;
		 
		for (int w = 1; w < iter; w++) //the product
		{
			if (w==iter) //k can't be j
			{
				break;
			}
			else 
			{
				denom = denom * (dcs[w]-dcs[q]); //multiply together to get the product
			}
			
		}
		
		sum = sum + (expo/denom); //add up all of the summations
		
	}
	
	totalmass = sum*consts*init;

	std::cout << totalmass;
	
	return 0;
}

Thanks again!
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Well, I can't run your program without the file it needs to read, but I'd suggest putting some cout statements throughout the code to print out the intermediate values.

- Warren
 
  • #3
I've attempted that, and I finally realized what the problems where this morning. Firstly, the first for loop should read "i<(iter-1)", and I shouldn't have included the break statement in the other for loop.
 
  • #4
Hi,
I see that you said your program worked. I am working with a few students to develope a detector but i also needed them to study the decan chains of Radon. I think your program will be useful, do you think i could get a copy of it?
 
  • #5
have you checked all of the values prior to use?
If so, are you trying to calculate a null with an int/double?
 
  • #6
Hi,
would it be possible to get a copy of the source code?
 

1. What is the Bateman Equation in C++?

The Bateman Equation in C++ is a mathematical formula used to model the decay of radioactive elements. It calculates the amount of a specific isotope present at a given time, taking into account the decay rate and the initial amount of the isotope.

2. How is the Bateman Equation implemented in C++?

The Bateman Equation can be implemented in C++ using a combination of mathematical operators and variables. The basic structure of the equation is as follows: N(t) = N0 * e-λt, where N(t) is the amount of isotope present at time t, N0 is the initial amount of the isotope, λ is the decay rate, and e is the base of the natural logarithm. The values for these variables can be input by the user or calculated within the program.

3. What are the inputs and outputs of the Bateman Equation in C++?

The inputs for the Bateman Equation in C++ are the initial amount of the isotope, the decay rate, and the time at which the calculation is being performed. The output is the amount of the isotope present at that given time.

4. Can the Bateman Equation in C++ be used for multiple isotopes?

Yes, the Bateman Equation can be used for multiple isotopes in C++. Each isotope would have its own initial amount, decay rate, and time input, and the equation would be calculated separately for each isotope.

5. What are some potential applications of the Bateman Equation in C++?

The Bateman Equation in C++ can be used in a variety of fields, including nuclear physics, environmental science, and archaeology. It can be used to model the decay of radioactive elements in a laboratory setting, track the spread of radioactive materials in the environment, and determine the age of artifacts containing radioactive isotopes.

Similar threads

  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
10K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top