November 7, 2006 --- Class 20---Hit or Miss Method, Inverse Transform Method Activities: Hit-or-Miss Method The Hit-or-Miss method and Sample Mean methods are described in Section 11.2. We reviewed the hit-or-miss method and detailed the sample mean method. We'll try the first method and see how well it does. However, first I showed an example of the converge of the trapezoid rule and Simpson's run. Trapezoid rule and Simpson's rule I did this example on my work computer and moved all the work to Nations using a tar (tape archive) file. To create a tar file, use tar -cvf myarchive.tar files or tar -cvf myarchive.tar directory c is for create, v is for verbose and f indicated that the next argument is the name of the tape archive file to be created. Then comes a list of files and/or directories that are to be include in the archive. To extract the files from the archive, you may use the command: tar -xvf myarchive.tar This unpacks the files from myarchive.tar and puts them in the current directory. If you used the second form above, then a directory will be created and the files placed there. In ~sg/integration_test/double_precision, you will find source code and output files. The source code has been modified so we can see how the method converges. The integral of cos(x) from x=0 to x=pi is calculated by xqsimp2. Here is the output: 1 7.853981633974e-01 3.333333333333e+29 2 9.480594489685e-01 1.002279877492e+00 3 9.871158009728e-01 1.000134584974e+00 4 9.967851718862e-01 1.000008295524e+00 5 9.991966804851e-01 1.000000516685e+00 6 9.997991943200e-01 1.000000032265e+00 7 9.999498000921e-01 1.000000002016e+00 8 9.999874501175e-01 1.000000000126e+00 9 9.999968625353e-01 1.000000000008e+00 10 9.999992156342e-01 1.000000000000e+00 The first column is the level at which we call the routine, i.e., with 2^j points in the integration range. The second column is the trapzoid rule and the third column is for Simpson's rule. Note that the j=1 value for Simpson's rule is bogus since we need to go at least to j=2 to use Simpson's rule. In cos.ax, you will find the error plotted vs. j. The crosses are for the trapezoid rule, the diamonds are for Simpson's rule. A second example integrates sqrt(1-x^2) from 0 to 1. Curiously, it looks very different. This is because of the singularity in the derivative of the integrand at x=1. Simpson's rule does not converge with a higher power of j. Plot qsimp.ax, or plot both graphs together. Testing Hit-or-Miss Method Program ~sg/src/pi3.c implements the hit-or-miss method using a feedback shift register random number generator. You have to tell the program how many "darts you want to throw." This is the first command line argument. You may also have it repeat the experiment multiple times. This is the optional second argument. Look at the code to see how this is done if you are unfamiliar with this type of feature. Also note how the random number generator gets its seed through a call to pid(). This is the "process id" of the calling routine. It is essentially a random number. If you like, you can modify the code so that you specify the seed yourself. This can be very useful when you are trying to debug the code and want to see if results are reproducible. Sometimes I set up my code so that it asks for a seed, but if the seed I give is negative, it calls pid() to get a random seed. That way, I can do many runs without having to think up new seeds, and when I want to reproduce runs with a known seed I can also do that. Call pi3 with the argument 50 and 500, and send the output to a file. Each line will have 50 (the number of darts) and the estimate of pi/4 (the value of the integral). Hist It is interesting to look at the distribution of the estimates. One way to do this is by making a histogram. You break the x-interval up into different ranges and then count the number of times a value falls in each range. hist is a program for doing this. I find it very useful. You are welcome to copy the source code from ~sg/src/hist.c. If you have a file with a bunch of numbers to histogram, just type hist.c