Question

In: Advanced Math

Write a matlab program that determines the value of pi using the monte carlo technique. do...

Write a matlab program that determines the value of pi using the monte carlo technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the computed value of pi and the difference from the true value as this number increases. Time the execution of your code for various numbers of points, and plot the precision vs the computational cost.

Solutions

Expert Solution

MATLAB CODE

% Write a matlab program that determines the value of pi using the monte carlo
% technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the
% computed value of pi and the difference from the true value as this number
% increases. Time the execution of your code for various numbers of points, and plot
% the precision vs the computational cost.
n = 1;
pimc = zeros(1,1);
exection_time = zeros(1,1);
for nTrial = 100:50:10000
tic
nHit = 0;
r = 1;
for i = 1:nTrial
x = r*rand();
y = r*rand();
if x^2 + y^2 <= (r)^2
nHit = nHit + 1;
end
end
pimc(n) = 4*nHit/nTrial;
exection_time(n) = toc;
n = n + 1;
end
figure(1)
plot(100:50:10000, pimc, 'LineWidth', 2);
xlabel('Number of trials', 'FontSize', 20)
ylabel('Monte carlo estimate of pi', 'FontSize', 20)
print -dpng figure1.png

figure(2)
plot(100:50:10000, pimc-pi, 'LineWidth', 2);
xlabel('Number of trials', 'FontSize', 20)
ylabel('pimc - pi', 'FontSize', 20)
print -dpng figure2.png

figure(3)
plot(exection_time, -log10(abs(pimc-pi)), 'LineWidth', 2);
xlabel('Execution time (s)', 'FontSize', 20)
ylabel('Precision', 'FontSize', 20)
print -dpng figure3.png


Related Solutions

Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is...
Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is NOT allowed.
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
What is the code for running the Monte Carlo integration technique for the integral of the...
What is the code for running the Monte Carlo integration technique for the integral of the standard normal distribution at 2? Please include a graph of sample size vs relative error in the solution.
using matlab can you show me an example of a monte carlo that has two things...
using matlab can you show me an example of a monte carlo that has two things randomizing?
in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius...
in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius 1. print out your code, at least one figure on which the circle and ‘dart hits’ are shown, and numerical results for N=10, 100,1000. For each N, repeat the calculation at least 5times.
A Monte Carlo simulation is a method for finding a value that is difficult to compute...
A Monte Carlo simulation is a method for finding a value that is difficult to compute by performing many random experiments. For example, suppose we wanted to estimate π to within a certain accuracy. We could do so by randomly (and independently) sampling n points from the unit square and counting how many of them are inside the unit circle (assuming that the probability of selecting a point in a given region is proportional to the area of the region)....
Perform Monte Carlo integration using R statistical programming to estimate the value of π. Generate N...
Perform Monte Carlo integration using R statistical programming to estimate the value of π. Generate N pairs of uniform random numbers (x,y), where x ∼ U(0,1)and y ∼ U(0,1) and each (x,y) pair represents a point in the unit square. To obtain an estimate of π count the fraction of points that fall inside the unit quarter circle and multiply by 4. Note that the fraction of points that fall inside the quarter circle should tend to the ratio between...
How to do the monte carlo method on a calculator? step by step method.
How to do the monte carlo method on a calculator? step by step method.
How and when do you use the Monte Carlo analysis? Does it have anything to do...
How and when do you use the Monte Carlo analysis? Does it have anything to do with understanding the probability of a risk occurring as well?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT