1.25

1.2 | 4 ratings Rate this file 5 Downloads (last 30 days) File Size: 959 Bytes File ID: #20235

Montecarlo simulation

by Clement Bernard

 

10 Jun 2008 (Updated 11 Jun 2008)

estimation of pi, using montecarlo method

| Watch this File

File Information
Description

%Draw a square of area 1 on the ground, then inscribe a circle within it.
%Now, scatter some small objects (for example, grains of rice or sand) throughout the square.
%If the objects are scattered uniformly, then the proportion of objects within the circle vs objects within the square should be approximately ?/4,
%which is the ratio of the circle's area to the square's area.
%Thus, if we count the number of objects in the circle, multiply by four,
%and divide by the number of objects in the square, we get an approximation to pi.

MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
11 Jun 2008 Dimitri Shvorob

This would impress in a kindergarten.

11 Jun 2008 Jos x@y.z

This needs much more internal comments, help, graphical output, etc. to be even slighly of interest as a (rather trivial) demonstration. Also make it a function like every proper demo in matlab.

12 Jun 2008 Heinrich Acker

I don't agree that this should be rated as 1 (=poor=Marco) just for being a simple demo of a well-known method. Positive about this contribution are:
- it works
- it can be useful as a demo for a method not everybody learns in kindergarten
- it has sufficient comments to allow the user to understand the method

It needs improvement, however:
- there should be line-by-line comments
- pi should be written as 'pi', it is displayed as '?' here
- the line 'pi=pi' doesn't make much sense
- why display axis([-1 1 -1 1]) if only one quadrant is used?
- 'axis equal' would make a circle look like a circle
- using smaller marker size would shift the problem that the points are shown as a blue square to higher n
- a demo would also benefit from better variable names

12 Jun 2008 Jos x@y.z

The reason I rated this as poor, is the following: this submission is understandable only for people who already know matlab and their maths. For everybody else this is completely above their head. I do not want to say that the idea is poor, it is just poorly implemented. Nobody will use this as it is

In my opinion it is not worth it to improve this submission, and I advise the author to start from scratch, after getting some more matlab skills. Remember that even the most professional programmer has written something like this when they were just starting, so do not be disencouraged. But learning to program doesn't mean you have to share everything you write.

12 Jun 2008 John D'Errico

No, this is not Marco "poor". But Marco really deserves a rating of -10 (pure dreck). This submission is still poor, although I'd be willing to give it a rating of about 1.25 if we could use a continuous scale for our ratings.

Why does this still deserve an rating of poor?

It is a script. As such it does not do anything useful for a user except as a teaching tool. And as a teaching tool, it is poor.

The code is amateurish, using command line input, a poor thing to teach others to use. It does things like overwrite the variable pi, a terribly poor thing to do, especially when the action is in a script! In fact, pi should never be overwritten as a variable. The author should also learn to use better variable names. A,B,C,e,p are completely arbitrary names, that tell the reader NOTHING about them. In a script that can have meaning ONLY as a teaching device, it is most important to use meaningful names.

The author needs to learn to write self-documenting code. This means to use variable names that actually mean SOMETHING. At the very least, put in a line of comment before each variable gets created that explains what was done.

Heinrich points out some of the many things that should be done better, so I won't re-list them all. There are others though. For example, the line

theta = 0:.1:2.*pi;

has theta running just short of pi, so the circle has a break in it. Better would have been to use linspace, creating a complete circle.

Even the last line of code is what I'd call poor. Why compute a relative deviation using the sqrt of a square of a difference between scalars?

dev = (sqrt((PI - pi).^2)/pi).*100

There is a new function in Matlab that does the same thing more efficiently, as well as more accurately. It is called abs, for absolute value. I don't know if this is found in all releases of matlab.

Even beyond the above comments, the code itself is pretty trivial.

For me, this just needs way too huge an amount of improvement to qualify as a 2.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
montecarlo Clement Bernard 22 Oct 2008 10:05:27
pi Clement Bernard 22 Oct 2008 10:05:27
stochastic Clement Bernard 22 Oct 2008 10:05:27
mathematics Clement Bernard 22 Oct 2008 10:05:27
general Clement Bernard 22 Oct 2008 10:05:27

Contact us at files@mathworks.com