Wow, visualfox.
That's another language I've used plenty.
Wish I could find another gig for that here in Portland -- maybe I will.
Is visualfox still used commercially a lot? I hear it's big in Prague -- but that was some years ago.
Back to Python, I've been helping this polytech student in Indonesia with his Monte Carlo integration. There's a simple Mathematica implementation I'm following, but I think my error term calc is screwed up.
If anyone here has the time to give me some pointers... help me spread Python user base in Indonesia!
CS teachers probably do this in their sleep, or for breakfast [some other idiom]. This could be another area where our DM track (digital math track) segues to calculus (always looking for those segues).
http://math.fullerton.edu/mathews/n2003/MonteCarloMod.htmlfrom random import randint
import math
def getpoints(n, a, b):
interval = b-a # assume a <= b
points = [] # empty list
for i in range(n):
r = randint(0,100) # some percent
point = a + (interval * .01 * r) # a plus some percent of interval
points.append(point)
return points
def g(x):
""" the function to evaluate for its definite integral over an interval """
# return x * x # or x ** 2
return math.sqrt(x)
def average(f, somepoints):
n = len(somepoints) # how many points
thesum = sum( [f(x) for x in somepoints])
return thesum/n
def geterror(f, guess, somepoints, a, b, n):
""" I have some questions about this """
thesum = (1.0/n) * sum( [f(x) * f(x) for x in somepoints]) # right?
return (b-a) * math.sqrt(abs(thesum - guess*guess) / n)
def montecarlo(f, n, a, b):
thepoints = getpoints(n, a, b)
theaverage = average(f, thepoints)
approx = (b - a) * theaverage
error = geterror(f, approx, thepoints, a,b, n)
return (approx, error)
def tests():
print getpoints(10, 1, 4)
print getpoints(10, 0, 10)
print average(g, getpoints(10, 0, 4))
print montecarlo(g, 100, 0, 4)
if __name__ == "__main__":
tests()
Kirby