MATLAB function that estimates pi

  • Context: MATLAB 
  • Thread starter Thread starter S_Flaherty
  • Start date Start date
  • Tags Tags
    Function Matlab Pi
Click For Summary
SUMMARY

This discussion focuses on creating a MATLAB function to estimate the value of pi by generating random points within a square and determining how many fall within a unit circle centered at the origin. The user seeks guidance on generating a 2 by n matrix of uniformly distributed random numbers within the range of -1 to 1, as well as plotting these points to visually represent the results. Key concepts include the relationship between the areas of the circle and square, which aids in approximating pi based on the ratio of points inside the circle to the total number of points generated.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Knowledge of random number generation in MATLAB
  • Familiarity with basic plotting techniques in MATLAB
  • Concept of geometric probability and area calculations
NEXT STEPS
  • Learn how to generate random numbers in MATLAB using the rand function
  • Explore MATLAB plotting functions, specifically plot and scatter
  • Study the mathematical relationship between the areas of a circle and a square to understand pi approximation
  • Investigate the use of loops in MATLAB for iterating through arrays and performing calculations
USEFUL FOR

Beginner MATLAB programmers, students learning about Monte Carlo methods, and anyone interested in numerical methods for estimating mathematical constants like pi.

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
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K