Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB function that estimates pi

  1. Sep 17, 2012 #1
    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.
     
  2. jcsd
  3. Sep 24, 2012 #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: Sep 24, 2012
  4. Sep 24, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MATLAB function that estimates pi
  1. Matlab input function (Replies: 0)

  2. Matlab cell functions (Replies: 0)

Loading...