# Monte Carlo Integration

### Introduction

In Monte Carlo integration, we choose random numbers to integrate over a certain range. An example we will simulate in this assignment is finding the area of a circle. Random numbers will be generated to integrate the area of a circle at the origin with radius r = 1. For every number generated that fits in the circle, it will be count as a "hit" and numbers that go outside the area will be a "miss." Below is an example graph of an integrated circle using Monte Carlo integration[1]:

Figure 1: Monte Carlo integration of a circle centered at origin with radius 1, iterated 3000 times

The red plots in the graph are random points generated that fit in this circle's radius and the green points are random points generated outside of the area. By accomplishing this in two dimensions, we can try and integrate a sphere in three dimensions. Then, we can also work with multi-dimension spheres called hyperspheres.

### Implementation

C++ Source files: Circle integration, sphere integration, hypersphere integration.

The drand48() function in the source codes generates random numbers for x and y values between [-1,1]. For the three dimensional sphere, the z values are included. In the hypersphere code, a nested for loop is used to generate values based on the number of dimensions entered in the program.

### Results and Graphical Analysis

Now that we know how a circle is integrated, let's take a look at a sphere in three dimensions:

 Figure 2: Sphere centered at origin with radius 1, repeated at 1000 trials Figure 3: Sphere centered at origin with radius 1, repeated at 30 000 trials

Print out of volume results:

 1000 Trials: Volume: 4.072 Actual volume: 4.18879 Fractional error: 0.11679 30000 Trials: Volume: 4.1704 Actual volume: 4.1887 Fractional error: 0.0183902

The figures above show that the accuracy of the integration method increases and the fractional error decreases as more trials are repeated. Additionally, the graphs below of the fractional error versus the number of trials does decrease as the number of trials go up.

 Figure 4: Fractional error vs trials for a 2D circle Figure 5: Fractional error bv trials for a 3D sphere

Now, let's take a look at the dimensions of hyperspheres. The following table lists hyperspheres with their volume from 2 to 10 dimensions. The graph below shows a plot of the volume versus the dimensions[2]. From the table, the largest volume at r = 1 is at 6 dimensions. After that, the volumes of the spheres start decresing.

In the program, we used this following equation[3] to calculate the volume of a hypersphere:

The table and graph below show the volume and error of the hyperspheres at 10,000 trials. In the table and the graph, it's obvious that as the dimensions increase, so will the error.

If we wanted to get an error precision of 10-5 for a 10-dimensional sphere, we may need to iterate the program 1012 times, however in my program I wasn't able to repeat the loop that many times because the number is too large. I tested that out using 108 trials and the volume came out to be 2.55016 with an error of 0.002976.

### Conclusion

From this assignment, we see that Monte Carlo Integration is possible with multidimensional spheres. We can probably use this same method to integrate more complex figures in multiple dimensions, such as a cube. The issue with accuracy is that more trials are needed as the dimensions increase, but sometimes that may be hard to compute because the program may not be able to allocate enough memory for large numbers. It will also take a while for the computer to finish processing the loops. I also noticed that as the dimensions increase, the volume of the hyperspheres decrease. Even though we only tested up to 10 dimensions, it's possible that the volume will keep decreasing and get close to zero.

### References

[1] PHYS 305 Assignment #6. Varner, Gary
[2] N-Dimensions. http://thinkzone.wlonk.com/MathFun/Dimens.htm
[3] Hypersphere http://mathworld.wolfram.com/Hypersphere.html