Apart from the above intuitive method of deducing the value of pi simply from the shape of a circle within a square, there are other useful applications of monte carlo sampling. For instance, we can sample repeatedly to deduce the area under the graph , ie. the integral of a function.
Problem: Evaluate the definite integral of the function for
This is shown by the area shaded in orange as shown in this image.
We can create a “box” to perform monte carlo sampling with
UniformDistributionOverBox which takes in a Real Interval, with dimensions (of the cyan box) as shown in the image above
To define Polynomials in s2, we use the following
Polynomial(3.0,5.0,0.0,0.0) will define the following polymonial
Some useful associate methods are available here :
- To get the corresponding result when x value is placed into the polynomial, we can use
xis the corresponding x value and
is the polynomial defined earlier saved as a variable
- We can also print the degree of polynomial (aka the power of the term with the largest effect on the outcome of the polynomial) via:
- We can get the coefficients of the various terms with :
.getCoefficients()for instance for the first term
0 of 1 Questions completed
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 1 Questions answered correctly
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Write code using the kotlin S2 to evaluate the definite integral of the function $latex x^3+x^2$ for $latex -1\le x \le 1$.
The Area between -1 and 1 of the function and the axis is . Give your answer to 2 d.p.
- What should be the area of the outside box which “contains” the function? You can adjust the size of the box by tweaking the upper and lower bounds of
- How do we check if a randomly generated dot is between the function and the x/y axis? We probably need to evaluate the value when a random x is placed into the polynomial
- What should the last line of code be to deduce the final value of the area? Hint: it involves a ratio of sorts.