PDA

View Full Version : Using Monte Carlo simulation to estimate Pi



I got this
02-06-2016, 01:28 AM
I'm in a intro to Python class and our assignment is to estimate Pi but I keep getting an error I don't understand. The error is

NameError: name 'win' is not defined

The reason I don't understand the error is that I've defined win in the
def create_win(w,h) function.
My code is:


from graphics import *
from math import *
import random

def generateXY(n):
xlist = []
ylist = []
for i in range(n):
xlist.append(random.uniform(-1,1))
ylist.append(random.uniform(-1,1))
return xlist, ylist


def create_window(w, h):
win = GraphWin("Monte Carlo",w,h)
win.setCoords(-1,-1,1,1)
return win

# create points list
def create_points(xlist, ylist):
points = []
for i in range(len(xlist)):
points.append((xlist[i], ylist[i]))
return points

# draw a circle with given radius
def draw_circle(w, radius):
center = Point(0,0)
c= Circle(center, radius)
c.draw(win)
return c
def draw_points(win, xylist):
for point in xylist:
p = Point (*point)
p.draw(win)

def distance_from_center(x,y):
return math.sqrt((x ** 2 + y ** 2))

# estimate pi using Monte Carlo simulation
def simulate_pi(win, xylist):
draw_points(win, xylist)
total_points = len(xylist)
points_inside = 0

for p in xylist:
if (distance_from_center(p[0],p[1]) <= 1):
points_inside += 1
percentError = points_inside/total_points
if percentError == .85:
return (8.22536130248883)
estimate_pi = 4 * (points_inside/total_points)
print(estimate_pi, percentError)

return percentError


def main():
xlist,ylist = generateXY(1000)
xylist = create_points(xlist, ylist)
win = create_window(500,500)
draw_circle(win, 1)
error = simulate_pi(win, xylist)
win.getMouse()
win.close
if __name__ == '__main__':
main()





Any help is much appreciated.

Thanks a bunch,
~I Got This

Ross
02-06-2016, 01:43 AM
I Got This

Oh okay nvm then

I got this
02-06-2016, 01:45 AM
Oh okay nvm then

Yeah my name is clearly inaccurate considering all my posts are asking for help <_<

Ross
02-06-2016, 02:23 AM
Yeah my name is clearly inaccurate considering all my posts are asking for help <_<

I'm just messin' man. Good luck!

Brandon
02-06-2016, 02:40 AM
I'm in a intro to Python class and our assignment is to estimate Pi but I keep getting an error I don't understand. The error is

NameError: name 'win' is not defined

The reason I don't understand the error is that I've defined win in the
def create_win(w,h) function.




def draw_circle(w, radius):
center = Point(0,0)
c = Circle(center, radius)
c.draw(win)
return c


Where did you define win? Is w supposed to be win? In this function, win is undefined and w is never used.

Turpinator
02-06-2016, 04:01 AM
this thread wouldnt be complete without a version in simba...
program pi;
const
n = $FFFFF;
var
i: integer;
begin
i := 0;
for 1 to n do
if (random()**2 + random()**2) <= 1 then
i := i + 1;
writeln('pi is approx ', i*4.0/n);
end.


def simulate_pi(win, xylist):
draw_points(win, xylist)
total_points = len(xylist)
points_inside = 0

for p in xylist:
if (distance_from_center(p[0],p[1]) <= 1):
points_inside += 1
percentError = points_inside/total_points
if percentError == .85:
return (8.22536130248883)
estimate_pi = 4 * (points_inside/total_points)
print(estimate_pi, percentError)

return percentError

So... im confused. why is percent error this? shouldnt the percent error be (estimate - pi)/pi ?
where did 8.22536130248883 come from?

Saint//+
02-14-2016, 04:32 AM
I'd say brandon is right.

Does it say what line the error occurred on?