In: Physics
I want to to calculate the area between the x-axis and the function f(x) = sin(x) on the interval [0, pi]. The area we seek is enclosed in a rectangle bounded by the curves. x = 0, x = pi, y = 0, y = 1. Since we know the area of the rectangle is pi, we can generate random points inside the rectangle and keep track of how many of those points lie below the curve y = sin(x). A bit of algebra involving ratios, and you can estimate the area under the curve.
For the purposes of this task, we can assume that we know the value of pi, as provided by Matlab's built in library of constants. We assume that we do not know the true value of the integral (area), and so as stopping criteria, we will use percent change. I want the process to terminate when [A(k) - A(k-1)]/A(k-1) is less than 0.01.
You should try running the program with an error tolerance of 0.001 just to see how much longer it takes. You may have to terminate the process, as getting that level of precision might take a great many random numbers.
In the above notation, A(k) represents the approximated Area when 10^k random points are generated. Use k values of 0, 1, 2, 3, etc. Non-integer values of k will produce a non-integer value of 10^k, and I don't now if your code could interpret that correctly.
for tolerence = 0.01
k | A(k) |
0 | 0 |
1 | 1.8850 |
2 | 1.9792 |
3 | 1.9918 |
k | A(k) |
0 | 0 |
1 | 1.5708 |
2 | 1.8850 |
3 | 2.0012 |
4 | 1.9893 |
5 | 2.0006 |
6 | 2.0020 |