#######################################
"""
The following is an example of how to use fmin
to approximate the minimum of a function.
"""
from scipy import array
from scipy.optimize import fmin
def F(Z):
# A function with a minimum at (x,y) = (1,2)
x = Z[0]
y = Z[1]
return (x-1)**2 + (y-2)**2
Z0 = array([0.,0.])
Zbest = fmin(F,Z0)
print("fmin finds a minimum at approximately... ", Zbest)
print("The actual minimum is at (1,2)")
#######################################
"""
Suppose we want to approximate the area under the parabola y = x**2 from 0 to
1. If we throw darts randomly at the square [0,1] by [0,1], then the
probability that the dart lands below the parabola is equal to the fraction of
the square's area below the parabola. So let's throw lots of darts, count how
many fall below, and use that to estimate the area. (we know from calculus that
as we collect more and more points our approximation should converge to 1/3rd,
the actual area under the parabola)
"""
from scipy import rand
numdarts = 100000
darthits = 0
counter = 0
while counter < numdarts:
counter += 1
x,y = rand(2) # where the dart hits
if y < x**2:
# the dart landed below the parabola, so we count it
darthits += 1
print(float(darthits)/float(numdarts))