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

  • Thread starter Thread starter lapo3399
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion revolves around troubleshooting a program designed to evaluate the Bateman equation for radioactive decay series using decay constants from a text file. The user is encountering an issue where the output is NaN (Not a Number) regardless of the nuclide selected. Key points include the need for adjustments in the loop conditions, specifically changing the first loop to "i < (iter - 1)" and removing a break statement in another loop. Suggestions are made to incorporate debugging outputs to track intermediate values. Additionally, there is interest from other users in obtaining a copy of the program for educational purposes, particularly for studying decay chains of Radon. The conversation emphasizes the importance of verifying input values and ensuring proper calculations to avoid null results.
lapo3399
Messages
53
Reaction score
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
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
 
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.
 
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?
 
have you checked all of the values prior to use?
If so, are you trying to calculate a null with an int/double?
 
Hi,
would it be possible to get a copy of the source code?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top