- #1
quark.antiquark
- 7
- 0
Homework Statement
I have to simulate a two-level electron scattering process. 1000 electrons start in state i and over time, end up in state f. I need to plot the number of electrons in each state as a function of time.
Homework Equations
None.
The Attempt at a Solution
The probability of going from state i to f is given. I had previously generated an array of random numbers between 0 and 1 (called xArray in the code below). At each time interval, I pick a random number and compare it to the probability. If the number is less than the probability, the electron goes to state f. Otherwise, it stays in state i. I do this for each electron in state i. Then, in the next time interval, I repeat the process, and so on. This all looks good on paper, but I'm not exactly the world's best programmer. My program seems to have a bug, and I've been trying to find it all evening. I end up with a negative number of electrons in the initial state, which doesn't make any sense. I've attached the relevant parts of my code (java); extraneous stuff like the random number generator have been left out for brevity. Hopefully someone will be able to spot the bug!
final double pf = 1./30.; // Probability of going to state f
int[] nfArray = new int[100000]; // Number in state f as a function of t
nfArray[0] = 0; // Start with 0 electrons in state f
int[] niArray = new int[100000]; // Number in state i as a function of t
niArray[0] = 1000; // Start with 1000 electrons in state i
int ni = 1000; // Number of electrons in state i at time t
int[] tArray = new int[100000]; // Elapsed time (for plotting purposes)
tArray[0] = 0;
int t = 1; // Time in ps
int j = 0; // Index of xArray
int k = 0; // # of electrons that have decayed (or not) at
// time t
while(ni > 0) // Stop when no electrons remain in state i
{
k = 0;
while(k < ni) // Loop through all electrons in state i
{
if(xArray[j] < pf) // If true, the electron goes to state f
{
nfArray[t] = nfArray[t] + 1; // One electron goes to state f
niArray[t] = niArray[t] - 1; // Also decrease the number in state i
}
else
{
; // The electron stays in state i
}
j++; // Pick the next random number
k++; // Go to the next electron in the system
}
ni = niArray[t]; // Number of electrons remaining in state i after time t
tArray[t] = t;
t++; // Next time interval
}
System.out.println("Time elapsed: " + tArray[t-1]);
System.out.println("Number in state i: " + niArray[t-1]);
System.out.println("Number in state f: " + nfArray[t-1]);