Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given a function f (x) y such that 0 k y c whenever a x b, we wish to estimate t

ID: 663317 • Letter: G

Question

Given a function f (x) y such that 0 k y c whenever a x b, we wish to estimate the area under f using a sampling procedure imagine the curve for f is enclosed in a rectangle with width b a and height c Generate n points randomly in this rectangle, and count how many points lie under the curve. The proportion of points that lie under the curve approximates A/B, where A is the area under the curve, and B is the area of the rectangle. From this we can estimate A. Clearly the precision of the estimate increases with the number of samples. Given a method with the signature public double f(double x) write a method public double area (double a, double b, double c int n) that implements the above procedure for f. You should use an object from the library class java.util. Random to generate a sequence of random points inside the rectangle, and you will need the following method from that ciass. public double nextDouble() Returns the next pseudorandom uniformly-distributed double between 0.0 and 1.0 from this random number generator's sequence.

Explanation / Answer

public double area(double a, double b, double c, int n)
{
   //generating sequence of points
   Random r = new Random();
   double[][] points = new double[n][2];
   for (int i = 0; i < points.length; i++) {
      points[i][0] = r.nextDouble();
      points[i][1] = r.nextDouble();
   }
   //counting number of points above curve
   int count = 0;
   for (int j = 0; j < points.length; j++) {
      if ((f(points[j][0]) - points[j][1]) < 0) {
         count++;
      }
   }
   double prop = count/n;
   double rectangle = (b - a) * c;
   double area = prop * rectangle;
   return area;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote