PRACTICE 1 : RADIOACTIVE DECAY Purpose: In this practice we tray to be familiar with WGNUPLOT (a free software graphic package) and QUICKBASIC, also free software for programming in BASIC language. This will provide the necessary background for the following practices. As specific objectives, we try to: - Use Wgnuplot to represent analytical functions. - Use QUICKBASIC to generate, capture and modify data files. - Use Wgnuplot to produce the graphical output of some data files. - Apply the finite differences technique to numerically solve the radioactive decay equations. Part I: Starting up with Wgnuplot. GNUPLOT is a command-driven interactive function plotting program. It is case sensitive (commands and function names written in lowercase are not the same as those written in CAPS). All command names may be abbreviated, as long as the abbreviation is not ambiguous. Any number of commands may appear on a line, separated by semicolons (;). Strings are indicated with quotes. They may be either single or double quotation marks, e.g., load "filename" cd 'dir' a) An overview. - Start up the program by double click in its icon - Go to help in the menu bar up. Select introduction and have a look to the different options. You can come back here every time you need some help. - Go to “file” in the menu bar up and select “demo”. Select the folder C:\gnuplot\demo and a list of demo files will be shown. Select “all.dem” and a galery with all the wgnuplot’s capabilities will be displayed (you can go from one to the following just selecting the option OK with the mouse, and finish with the option CANCEL). b) Our first plot of an analytical function. plot and splot are the primary commands of the program. They plot functions and data in many, many ways. plot is used to plot 2-d functions and data, while splot plots 3-d surfaces and data. Syntax: plot {ranges} {<function> | {"<datafile>" {using ...}}} {title} {style} {, <function> {title} {style}...} splot {ranges} {<function> | {"<datafile>" {index i} {using ...}}} {title} {style} {, <function> {title} {style}...} where either a <function> or the name of a data file enclosed in quotes is supplied. A function is a mathematical expression, or a pair (plot) or triple (splot) of mathematical expressions in the case of parametric functions. User-defined functions and variables may also be defined here. plot and splot commands can be as simple as plot sin(x) or as complex as (!) plot [t=1:10] [-pi:pi*2] tan(t), "data.1" using 2:3 with lines, t**2 with points Example: Plot the function N=N0 e-t for the particular values of N0 = 100 -1 (arbitrary units) and = 0.031 y ( value for lead-210) and for a time interval from 0 to 120 years. Note that the independent variable in wgnuplot has to be term , necessarily, x. So we have to write plot [0:120] 100*exp(-0.031*x) After validating with the return key, the plot appears in the screen. Close the window and return to the wgnuplot mainscreen. If you press the up arrow key ( ) the last sentence will be rewriten and you can validate again, if you want. This way you can come back to any previous command line you wrote before without need to rewrite it again. To get again the plot you also can type replot and validate of just press this opotion in the upper-bar menu. To complete our plot we can use some of the following options (you can follow the effect of each one by using the replot option) : - Add x and y labels set xlabel “Time (y)” set ylabel “Pb-210 (a.u.)” - Add a title set title “Lead-210 radioactive decay” - Add a grid set grid - Chage the relative size of x and y axes set size 1,.8 - Readjust the range of the axis set xrange [0: 100] set yrange [0:150] - Select logarithmic scale set logscale y Part II. Solving the decay equation by finite differences. To solve the radioactive decay equation we will apply the definition of derivative, thus dN (t ) N (t t ) N (t ) dt t in the limit of t0. This limit will be approached by a t small enough. N (t t ) N (t ) N (t ) N (t t ) N (t ) t N (t ) t (1) Providing the initial conditions , N(t=0)=N0 , we can explicitly solve the number of atoms remaining after a time interval t. Then, as N(t=t) is already know we can apply again Eq. 1 to solve N(t=2t), and so on for any t. This iterative process can be easily programmed in a computer code, as the QUICKBASIC code that follows: OPEN "decay1" FOR OUTPUT AS #1 lambda = LOG(2) / 22.3 dt = .1 t = 0 Nold = 100 LET lt = 120 / dt FOR s = 1 TO lt t = t + dt Nnew = Nold - lambda * dt * Nold Nold = Nnew IF ABS(INT(s / 10) - s / 10) = 0 THEN PRINT #1, t, Nnew NEXT s CLOSE #1 END Lets us discuss each line: OPEN "decay1" FOR OUTPUT AS #1 An ASCI file with the name decay1 (you can include any valid extension within the two “, ie. “decay1.dat” ) will be opened by the code with a canal of communication (channel #1) through which will be introduce data each time that a sentence “PRINT #1, ….” was used. lambda = LOG(2) / 22.3 We define the variable lambda and a numerical value is associate to it (the number can be written directly –ie. 0.2345 – or provided by any valid mathematical expression, as in this case. This value corresponds to the radioactive decay constant of the 210Pb. Note that QUIKBASIC uses the LOG(x) function for the neperian logarithm, thus LOG(2) = Ln2 dt = .1 t = 0 Nold = 100 LET lt = 120 / dt A variable dt is defined and the value .1 is assigned to it. This will be our t =0.1 year used to solve the equation. We will discuss elsewhere the choice of this t. The variable t will be used to count the time, and initially the value 0 is assigned. We will use the notation Nold for N(t) and Nnew for N(t+t). Giving Nold=100 we are providing the initial conditions (100 atoms, mols, g or any valid unit). The sentence LET is used to define a variable, but it can be equally omitted . lt will be the number of iterations needed to solve our equation, what depends on the desired duration of the simulation (120 years in our case) and on the time step used (dt). In this case we will need 1200 iterations. FOR s = 1 TO lt This sentence along with NEXT s defines a cycle or curl that will be repeated lt times (more specifically, counting from s= 1 to lt with step 1 – assumed by default- ). For each value of s, the code is executing all the command lines comprised between the FOR an the NEXT statements. t = t + dt Nnew = Nold - lambda * dt * Nold (2) Each time we are doing an iteration, the time will be increased in dt. The second sentence gives us the explicit solution of N(t+t)=Nnew (compare with Eq. 1). Thus, for s=1 t=t+dt= 0+dt=dt, Nold= N0 (the initial condition) and we solve for Nnew=N(0+dt). Nold = Nnew With this change we can use exactly the same equation (2) to solve N(t=2dt) from N(t=dt). IF ABS(INT(s / 10) - s / 10) = 0 THEN PRINT #1, t, Nnew This sentence produces the output. The most relevant part is the statement PRINT #1, t, Nnew. This means that using the channel #1, the code will write in the file decay1 a line with the actual values of t and Nnew. If we do so for each s we will get a very large file with 1200 lines. We think that it’s enough to print only one of each 10 values. Note that the equality ABS(INT(s / 10) - s / 10) = 0 is verified by the multiples of 10 (10,20,30,40, ….) and only IF SUCH CONDITION IS VERIFIED the rest of the statement will be accomplished (this is, to write in decay1 file ….). Note that INT( ) is the integer part of a number and ABS is the absolute value. NEXT s CLOSE #1 END Next s forms part of the FOR NEXT sentence already commented. After the last s value (s=lt) the code exits the FOR NEXT cycle and continues with the following sentences. CLOSE #1 is used to close this channel of communication, closing and saving the file decay1 since we wont need it any further. END is the last sentence and the programme stops, If we edit the output file “decay1” we can see a two column data file (the first part is reproduced here): 1 2 2.999999 3.999998 4.999998 5.999997 6.999996 7.999995 8.999998 10 96.93483 93.96362 91.08348 88.29163 85.58534 82.962 80.41908 77.95411 75.56469 73.2485 Note that we expected outputs for t=10*dt =1 y, 2 y, 3 y, etc. Nevertheless we got some numerical errors of the order of a part per million or less. These numerical errors are not important in our case, but they can be in other cases. Sometimes we can use double precision in our computations. Exercise: Use the windows’ explorer to find the folder PRACT1 and open the QBASIC code. This software has an iterative help that you can use when needed. Write the sentences of the code that solves the radioactive decay for the 210Pb. Use the menu file and select the option save as… to save your work. Use any name, as DECAY.BAS (the quickbasic codes need the extension .BAS). Now select the option RUN in the upper-bar menu and select START. The code will be executed in few seconds. Then exit the code (option inside the FILE menu) A decay1 file has been created. Part III plotting data files with wgnuplot. Initiate Wgnuplot. Select in the menu FILE (the upper-bar menu) the option CHANGE DIRECTORY and write the full address of the folder PRACT1. Observe the line of command that is automatically generated (next time you can write it directly if you prefer). Write and execute the command: plot “decay1” Wgnuplot will represent by default the data contained in the first two columns (the only two in our case) of the file “decay1” assuming that x are in the first column and y in the second. By default it will use points to represent each (x,y) couple. plot “decay1” w l is doing the same but connecting the points with lines (w l) Try also plot “decay1” , “decay1” w l You can add labels, titles, etc. as in the case of analytical functions. At this point one can think that the finite differences is only a “rough approach” to the true analytical solution. We will study elsewhere more about the finite differences technique, but lets now compare by ourselves the numerical against the analytical solutions just plotting: plot “decay1”, 100*exp(- 0.031082833*x) ¿What are your preliminary conclusions? Part IV. Let’s be creative… Develop a QUICKBASIC code to solve the radioactive decay equations for a parentdaughter system (for example 226Ra and 222Rn with half-lives of 1600 y and 3.84 days , respectively). Produce a “decay2” output file containing four column of data: t, Nnewp (parent), Nnewd (daughter), Nnewp+Nnewd Plot the solution with wgnuplot using the sentence: plot “decay2” using 1:2 wi li, “decay2” us 1:3 wi li, “decay2” us 1:4 w l us (using) 1:3 means using column 1 as x and column 3 as y Try also this option: plot “decay2” us 1:2 title “Ra-226” w l, “decay2”,…..