use c to calculate monte carlo pi approximation and plot thr

use c++ to calculate monte carlo pi approximation and plot three graphs for N=[100,1000,10000]. plot the points inside the circles in red and points outside the circle in blue.

Solution

#include #include #include #define _USE_MATH_DEFINES using namespace std; void values(float *pi, int *P, int *Q) { float x(0), y(0); x = (rand()%100)/99.0; y = (rand()%100)/99.0; if(sqrt((x*x)+(y*y))<=1) { (*P)++; } (*Q)++; *pi = 4.0*(*P)/(*Q); } float get_pi(float required_accuracy) { int D(0), N(0); float pi_estimate(0), x(0), y(0), temp(0); while(N<=100) { values(&pi_estimate,&D,&N); } while(1) { if(4./(N+1.)<((required_accuracy*0.01)*(pi_estimate))) { cout<=1.e-14;a/=10.) { cout<<\"Actual accuracy : \"<<(fabs(get_pi(a) - M_PI)/(M_PI))*100.<

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site