Help is appreciated, please and thank you. Objective: Implement a Monte Carlo me
ID: 3602945 • Letter: H
Question
Help is appreciated, please and thank you.
Objective:
Implement a Monte Carlo method in C++ to estimate the value of .
Background:
Monte Carlo methods repeatedly perform calculations using random numbers to estimate a numerical result. For example, the value of may be estimated by “throwing” darts randomly at a dartboard.
Consider randomly “throwing” darts at a square with sides of length 1.0 and a quarter circle inscribed. The probability of a randomly thrown dart hitting inside the quarter circle is equivalent to the area of the circle divided by the area of the square. The area of the quarter circle is /4 and the area of the square 1.0. Therefore, the probability of hitting the quarter circle is /4.
Figure 1 A circle of radius, r=1, inscribed in a square with sides of length, 2.0 and a quarter or the circle inscribed in a square with sides of length, 1.0.
Write an algorithm to “throw” darts at the small square with the quarter circle and compute the probability of hitting inside the circle. Use the computed probability to estimate . Use a “top-down” approach to design your program and implement each subtask as a function. Include at least the following functions:
A function called random_double_generator() that takes no arguments and returns a random double between 0 and 1, inclusive.
An overloaded function called random_double_generator(double min, double max) that takes the two arguments, min and max, and returns a random double between min and max, inclusive.
A function called euclidian_distance(double xvalue, double yvalue) that returns the distance for the (x,y) pair provided to the origin.
An overloaded function called euclidian_distance(double x0, double y0, double x1, double y1) that returns the distance between the points (x0,y0) and (x1,y1).
A function called percent_difference(double acceptedValue, double estimatedValue) that returns the absolute value of the percent difference between the two values.
Optional: Repeat this for a unit circle centered at (1,2) using your overloaded functions.
Explanation / Answer
#include <iostream>
#include<math.h>
#include<cstdlib>
using namespace std;
double random_double_generator() {
double *x;
*x=1.00;
return modf(rand(),x);
}
double random_double_generator(double min, double max) {
double *x;
*x= max - min + 1.0;
double y = modf(rand(),x);
return y + min;
}
double euclidian_distance(double xvalue, double yvalue) {
//origin is (0,0))
return sqrt((xvalue)*(xvalue) + (yvalue)*(yvalue));
}
double euclidian_distance(double x0, double y0, double x1, double y1) {
return sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0));
}
double percent_difference(double acceptedValue, double estimatedValue) {
return ((acceptedValue - estimatedValue) / ((acceptedValue + estimatedValue) / 2))*100;
}
int main() {
//you can yourself write your main method using the above functions as per your requirement
//I am just giving you the definition of the required methods
//Good Luck!!
cout << random_double_generator() << endl;
cout << random_double_generator(0, 1) << endl;
cout << euclidian_distance(1, 1) << endl;
cout << euclidian_distance(0, 0, 1, 1) << endl;
cout << percent_difference(1, 1.5) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.