Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.6K Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Avoid issues with a simple Monte-Carlo problem?

Posted 6 years ago

Have a figure: x^4 + y^4 + 62x^2y^2=1 inside {x,-1,1},{y,-1,1} want to "estimate" the area of the figure inside using random dots.

dots = RandomReal[{-1, 1}, {1000, 2}];

tester[x_, y_] := x^4 + y^4 + 62*x^2*y^2 <= 1;

infigure = Select[dots, tester];

notinfigure = Complement[dots, infigure];

p1 = ContourPlot[{x^4 + y^4 + 62*x^2*y^2 == 1}, {x, -1, 1}, {y, -1, 
    1}];

p2 = ListPlot[{infigure, notinfigure}, PlotStyle -> {Green, Blue}];

here I'm getting an error with the plotlist, it generates numbers within -1,1 and then says they are not numbers...

Show[p1,p2]

What am i doing wrong? I did this with the unit circle and it shows fine.

POSTED BY: Joel Nabi
5 Replies

you must post code with the code button (second from the upper left) or the text will be altered (missing underscores, etc.)

POSTED BY: Neil Singer
Posted 6 years ago

i had it as tester[{x,y }] with underscore it wont let me do underscore here for some reason.

nothing changed.

POSTED BY: Joel Nabi

Joel,

Your bug was identified, but you could also do it with ImplicitRegion (this is a bit faster than the posted approach although the difference is insignificant)

reg= 
 ImplicitRegion[x^4 + y^4 + 62 x^2 y^2 <= 1, {x, y}];
pts = RandomReal[{-1, 1}, {10000, 2}];
inside = Count[RegionMember[reg, pts], True];

Regards,

Neil

POSTED BY: Neil Singer

I'm guessing tester[x, y] should instead be tester[x_, y_].That is to say, pattern variables instead of literal matches to symbols x and y.

POSTED BY: Daniel Lichtblau
Posted 6 years ago

Try changing

tester[x, y] := x^4 + y^4 + 62x^2y^2 <= 1;

to

tester[{x_, y_}] := x^4 + y^4 + 62x^2y^2 <= 1;

and if that works then study that until you understand that change.

There are other ways you could accomplish the same result.

POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard