MATLAB MATLAB function that estimates pi

Click For Summary
The discussion revolves around creating a MATLAB function to estimate the value of pi using random points within a square that contains a unit circle. The user is new to MATLAB and seeks guidance on generating random points within the specified range of -1 to 1 for both x and y coordinates. They express difficulty in plotting these points and wonder if the assignment can be completed without visualization. The conversation highlights the importance of generating a 2 by n matrix of uniformly distributed random numbers and utilizing MATLAB's plotting functions to distinguish points inside the circle from those outside. Additionally, there is a focus on understanding how the ratio of points within the circle to the total number of points can be used to approximate pi, emphasizing the relationship between the areas of the circle and the square. The user is encouraged to refine their search strategies for MATLAB-related queries to improve their programming skills and problem-solving approach.
S_Flaherty
Messages
75
Reaction score
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
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:
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?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K