MATLAB function that estimates pi

In summary, the goal of this conversation is for the individual to generate a large number of random points on a plot and find an approximation for the value of pi. They suggest looking into MATLAB functions that generate random points and working with a matrix to approximate pi. They also suggest that plotting might be helpful in this process.
  • #1
S_Flaherty
75
0
I have to write a MATLAB function that estimates the value pi based on the fraction of a collection of random points in the square [1; 1] that are within the circle of radius 1 centered at the origin.

I am very new to MATLAB and computer science in general and I've been having a great deal of difficulty figuring things out. I know that I need to create a function that generates a large number of random points on a plot where both the x and y-axis are from -1 to 1. With those points I know that I need to make it so that the points within the unit circle with a center at 0,0 are distinguishable from the other remaining points, and finally I need to find out the value of pi using those points within the circle. I've been trying to figure out how to at least make a plot with more than 1 random point but I can't even do that. Can someone help me understand how to make MATLAB generate multiple random points within the parameters mentioned above? And after I've figured out how to do that maybe I can understand how to approximate the value of pi from all of this.
 
Physics news on Phys.org
  • #2
Hints for how I would attack this.

First I need to know how to generate a 2 by n vector of uniformly distributed random numbers. So I Google
matlab random number
and find some really helpful web pages.

Next you are talking about plotting. Plotting sounds complicated. I wonder whether I could satisfy the assignment without plotting. So I wonder what I would need to do with that 2 by n matrix that would be enough to satisfy the assignment. I Google
matlab for loop
or
matlab matrix function
and look at anything that might give me a hint how I could use that matrix to come up with a number that seems to be the goal of you assignment.

But if you really want to plot then again I turn to Google and
matlab plot points
gives me some interesting places to start reading.

When I'm using a programming language with a distinctive name I use that name as the first word in my Google search. That focusses the search. The next word probably needs to be the most essential word that defines the task. Perhaps one or at most two more help fine tune the process and hopefully that will get you to a handful of promising pages that will tell you the answer. (Long long ago, before Google, the table of contents or index in the paper manual that came with the language was used to accomplish this).

That isn't telling you the code to do your project, in fact your project might have already been due, but it is trying to give you hints about how I think when I need an answer to a question.

ProgrammingByGoogle isn't really a great idea and shouldn't be your end goal. But, sort of like training speech recognition software, "training" Google to find the right pages for you is just as much training you to know what words to think of immediately when you are looking at a new programming assignment. Hopefully with a little practice you will have somehow soaked up the intuition that makes programming work.
 
Last edited:
  • #3
S_Flaherty said:
I know that I need to create a function that generates a large number of random points on a plot where both the x and y-axis are from -1 to 1. And after I've figured out how to do that maybe I can understand how to approximate the value of pi from all of this.
If the size of the square is 1 by 1, centered at the origin, then the x and y values should range from -.5 to +.5. You might want the size of the square to be 2 by 2, so that x and y values range from -1 to +1. Note that the area of the circle is π r2, and the area of the square is 4 r2, so how could this information about the areas help you to appoximate pi?
 

1. How does the MATLAB function estimate pi?

The MATLAB function uses a Monte Carlo simulation method to estimate pi. This method involves generating random points within a square and counting the number of points that fall within a quarter of a circle inscribed within the square. By using the ratio of the number of points inside the circle to the total number of points, pi can be approximated.

2. Is the estimated value of pi accurate?

The estimated value of pi from the MATLAB function will always be an approximation, as it is impossible to generate an infinite number of random points. However, the more points that are generated, the more accurate the estimation will be.

3. How can I improve the accuracy of the estimated value of pi?

To improve the accuracy of the estimated value of pi, you can increase the number of random points generated in the simulation. This will result in a more precise estimation of pi.

4. Can the MATLAB function estimate pi to a specific number of decimal places?

Yes, you can specify the number of decimal places for the estimated value of pi in the MATLAB function. This can be done by adjusting the number of random points generated in the simulation or by using the "format" function in MATLAB to specify the number of decimal places.

5. Are there any limitations to using the MATLAB function to estimate pi?

One limitation of using the MATLAB function to estimate pi is that it relies on random numbers, which can introduce a degree of uncertainty. Additionally, the estimated value of pi will always be an approximation, and it may not be as accurate as other mathematical methods for calculating pi.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
737
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top