# ex_pi_estimation.ppl
#
# The code in this file is part of Pyxplot
# <http://www.pyxplot.org.uk>
#
# Copyright (C) 2006-2012 Dominic Ford <coders@pyxplot.org.uk>
#               2008-2012 Ross Church
#
# $Id: ex_pi_estimation.ppl 1261 2012-07-11 21:38:05Z dcf21 $
#
# Pyxplot is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# You should have received a copy of the GNU General Public License along with
# Pyxplot; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA  02110-1301, USA

# ----------------------------------------------------------------------------

# This script produces a figure for the Pyxplot Users' Guide

# Call common initialisation script
reset
title = "null"
load "examples/fig_init.ppl"

set seed 0
width = 7

# Make a table of random numbers

# BEGIN
Nsamples = 500

rand() = random.random()

set samp Nsamples
set output "examples/eps/ex_pi_estimation.dat"
tabulate 1-2*rand():1-2*rand() using 0:2:3

n=0
foreach datum i,j in "examples/eps/ex_pi_estimation.dat" u 2:3
 {
  n = n + (hypot(i,j)<1)
 }
print "pi=%s"%(n / Nsamples * 4)
# END

# BEGIN
set multiplot ; set nodisplay

# Draw a unit circle and a unit square
title = "ex_pi_estimation" ; load "examples/fig_init.ppl"
box from -width/2,-width/2 to width/2,width/2
circle at 0,0 radius width/2 with lt 2

# Now plot the positions of these random data points andi
# count how many lie within a unit circle
n=0
foreach datum i,j in "examples/eps/ex_pi_estimation.dat" using 2:3
 {
  point at width/2*i , width/2*j with ps 0.1
  n = n + (hypot(i,j)<1)
 }
set display ; refresh
print "pi=%.4f"%(n / Nsamples * 4)
# END

# Call common cleanup script
load "examples/fig_end.ppl"

