Question

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 fo...

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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab code for Monte Carlo simulation for finding area of circle clear all close all Radius of the circle % number of pointsclear all Radius of the circle r-1: % number of points generated N = 100;x = a + (b-a ) . * rand N , 1 ) ; y-a (b-a).randN,1); radii - sqrt(x.2+y.2); hits sum4 rad11<=1 } ; th=linspace(0,2-pi); xclear all もRadius of the circle r-1i t number of points generated N 1000 ar b = r; xa(b-a) rand(N,1)1 ya (b-a). rand (N,1) hifigure( 3) plot (x r,y r, Linewidth,2) Plotting data k-0 hold orn for i=1:1 if radd<-1 xx(k)랭(i); YY(k) yi) plot(x(i),y(i),Monte Carlo plot for N 10 0.8 06 0.4 0.2 0.2 0.4 0.6 -0.8 1 08 6 04 204 08 08Monte Carlo plot for N-100 08 0.6 0.4 0.2 24 0.2 -0.6 0.8 O.4 1 080.60402 02 040 08Monte Cario plot for N 1000 参 + 04 1 -0806 0402 02 04 08 O8

%%Matlab code for Monte Carlo simulation for finding area of circle
clear all
close all
%Radius of the circle
r=1;
% number of points generated
N = 10;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(1)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
clear all

%Radius of the circle
r=1;
% number of points generated
N = 100;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(2)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
clear all
%Radius of the circle
r=1;
% number of points generated
N = 1000;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(3)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
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 fo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

  • Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

    Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task For this assignment you will simulate a given number of hold?at?N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user...

  • I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the...

    I wrote a program which computes the area and perimeter of a square, circle, or rectangle. As you will see in my main function, there is a for loop in which the user is supposed to be able repeat the program until they enter "q" to quit. However, my program runs through one time, the prompt appears again, but then it terminates before the user is allowed to respond to the prompt again. I'm not able to debug why this...

  • This is a matlab HW that I need the code for, if someone could help me figure this out it would b...

    This is a matlab HW that I need the code for, if someone could help me figure this out it would be appreciated. The value of t can be estimated from the following equation: in your script file, estimate the value of π for any number of terms. You must ask the user for the desired number of terms and calculate the absolute error/difference between your calculation and the built-in MATLAB value ofpi. Display your results with the following message...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

  • A. Introduction and Objective Every test has at least two sources of variation that affect the results of...

    A. Introduction and Objective Every test has at least two sources of variation that affect the results of the test. The first source of variation is due to the experimental procedure, such as using two different testing machines that have different calibrations, or different observers reading the same equipment differently. This type of variation is often called the experimental error The second source of variation is inherent in the specimens (or sample population) themselves. In other words, no two specimens...

  • Consider a cylindrical capacitor like that shown in Fig. 24.6. Let d = rb − ra...

    Consider a cylindrical capacitor like that shown in Fig. 24.6. Let d = rb − ra be the spacing between the inner and outer conductors. (a) Let the radii of the two conductors be only slightly different, so that d << ra. Show that the result derived in Example 24.4 (Section 24.1) for the capacitance of a cylindrical capacitor then reduces to Eq. (24.2), the equation for the capacitance of a parallel-plate capacitor, with A being the surface area of...

  • please use python and provide run result, thank you! click on pic to make it bigger...

    please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT