i'm trying to creat a simple script to find the area of a triangle using monte carlo simulation. however, i'm basically stuck. could someone please help me.
any help is appreciated,
thannks

 Réponse acceptée

Roger Stafford
Roger Stafford le 10 Fév 2014

0 votes

The theory of the Monte Carlo simulation which you describe is, I presume, that if you enclose a triangle completely in a square of known area, the area of the triangle can be estimated by multiplying the area of the square by the fraction of the random points within the square which happen to land inside the triangle. Of course this requires a very large number of random points to obtain a reasonably accurate estimate.
With the triangle you have specified with vertices (0,0), (0,1), (1,0), it is very easy to do this, and in fact you have done just that in your code. Writing x = rand and y = rand will fill the unit square with vertices at (0,0), (1,0), (1,1), and (0,1) uniformly. Since x and y are non-negative, the only test you need for being inside your triangle is for the hypotenuse, whether or not x+y <=1, and that is what your 'count' is recording. That means your code as you have shown it is almost done. Multiply the known area of the square by the fraction count/tries and you have your estimate for the area. You were almost there!
The simulation is a little more challenging with an arbitrary triangle. You have to define a square which is guaranteed to completely enclose it and then generate a large number of random points that are distributed uniformly in the square. Each of these points is tested to see if it does or does not lie within the triangle. The fraction that do lie within it is to be multiplied by the area of the square to give an estimate of the triangle's area.

1 commentaire

Thufz
Thufz le 10 Fév 2014
Modifié(e) : Thufz le 10 Fév 2014
exactly what i was looking for
thanks

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 9 Fév 2014

0 votes

You have not specified the 3 vertex coordinates of the triangle. Once you do that you can use inpolygon() to determine if a test point is inside the triangle.

4 commentaires

Thufz
Thufz le 9 Fév 2014
how would i go about doing that
Tell me what the vertices are. Tell me what x1, y1, and x2,y2, and x3,y3 are. Then make xv and yv like this
xv = [x1, x2, x3];
yv = [y1, y2, y3];
Then see if x,y is in or out
itsInside = inpolygon(x,y,xv,yv);
If you read the help for inpolygon, you should know how to do it.
Thufz
Thufz le 9 Fév 2014
Modifié(e) : Thufz le 9 Fév 2014
the vertices are (0,0), (0,1), (1,0)
and how will i calculate the area using what you have said perviously
Image Analyst
Image Analyst le 9 Fév 2014
Modifié(e) : Image Analyst le 9 Fév 2014
x = [0, 0, 1]
y = [0, 1, 0]
area = polyarea(x, y)
However I don't know how a Monte Carlo could be worked into this. The only way I can see it is if you run it a million times and get random coordinates, but I don't know if that is what you are after.
By the way, don't use max as a variable name or else you will destroy a very important build in function. Same for min.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by