Question
write a MATLAB script using the provided template: %% disp('*****problem 1*****'); disp('Estimation of pi by Monte Carlo method') %Generate N random points in the first
write a MATLAB script using the provided template:
%% disp('*****problem 1*****'); disp('Estimation of pi by Monte Carlo method')
%Generate N random points in the first quadrant N = 1000; %for the first case; change N for subsequent cases
%Calculate the distance from the origin of each point
%Estimate pi based on the fraction of points that are within the circle %Use these variable names if using the checker
pi_1 =0; pct_err_1 = 0;
%model of suggested method to display results
disp(['N = ', num2str(N), ', pi = ', num2str(round(pi_1, 4))]) disp(['% error = ', num2str(round(pct_err_1,2))])
%repeat for larger N values
%For this problem it's OK to duplicate code. Use variable names pi_2, pi_3, %pi_4, pct_err_2, etc. % %If you recall how to use a loop to avoid duplicating code for the 4 test cases, you may do so, %but it's not expected. However, it is NOT acceptable to use a loop + if %structure instead of logical array operations to perform the calculation.
Monte Carlo method for determining 1. Imagine that a large number of points are chosen at random within a square, as shown in the figure below. Inscribed within the square is a circle. Suppose that you count the number of points within the circle (the red points in the figure) and the total number within the square. Assuming that the points are distributed evenly throughout the square the fraction, the fraction that lies within the circle would be equal to the ratio of the area of the circle to the area of the square. That is, r? Nred (2r)2 Ntotal 4 width = 2r https://www.101computing.net/estimating-pi-using-the-monte-carlo-method/ Thus, an estimate for T can be calculated by generating a large number of points within a square at random, determining what fraction of those points lie within the circle, and multiplying by 4. Generate N points in the (x.y) plane by using rand () to create two arrays of random numbers called x and y. (Remember that rand() generates uniformly distributed random numbers between 0 and 1. Thus r =1, but all of the generated points will lie in the 1st quadrant of the figure. That doesn't change the calculation.) Calculate r? = x? + y?. Using a single logical array operation, determine how many of the points lie within the unit circle (r?Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started