I want to to calculate the area between the x-axis and thefunction f(x) = sin(x) on the interval [0, pi]. The area we seek isenclosed 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 cangenerate random points inside the rectangle and keep track of howmany of those points lie below the curve y = sin(x). A bit ofalgebra involving ratios, and you can estimate the area under thecurve.
For the purposes of this task, we can assume that we know thevalue 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. Iwant the process to terminate when [A(k) - A(k-1)]/A(k-1) is lessthan 0.01.
You should try running the program with an error tolerance of0.001 just to see how much longer it takes. You may have toterminate the process, as getting that level of precision mighttake a great many random numbers.
In the above notation, A(k) represents the approximated Areawhen 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 of10^k, and I don't now if your code could interpret thatcorrectly.