[Tutor] Re: Tutor digest, Vol 1 #2302 - 11 msgs
Magnus Lycka
magnus@thinkware.se
Wed Mar 5 20:22:03 2003
Mic Forster wrote:
>TypeError: fun() takes exactly 2 arguments (1 given)
> >>>
I think the error message is very clear. :)
The bisect function provides one variable to the function
call. What I did in my original example, if I remember
coerrectly, was to do like this (although the values were
different then):
s = 33
j = 1. / 7861
def fun(k):
return f(k, s) - j
delta = 1e-9
bisect(delta, 1-delta, delta, fun)
You could design the bisect function so that it takes more
parameters, but it gets more complicated.
If you don't like these global variables, you could use
lambda I guess... I'll give you two versions, without and
with lambda...
def bisect(min, max, delta, function):
fMax = function(max)
fMin = function(min)
assert fMax * fMin < 0, locals()
while max - min > delta:
newX = 0.5 * (min + max)
fNew = function(newX)
if fNew * fMax > 0:
max, fMax = newX, fNew
else:
min, fMin = newX, fNew
return newX, fNew
def f(k, s, j):
# I'll stick to the old equation
return k / (1-k) * (1-k)**s / (1-(1-k)**s) - j
delta = 1e-9
# No lambda
for s, jInv in [(11, 862), (33, 7861)]:
j = 1./jInv
print "j = %f s = %i" % (j,s)
def fun(k):
return f(k, s, j)
print "k = %.17f +/- %e" % bisect(delta, 1-delta, delta, fun)
print
# With lambda
for s, jInv in [(11, 862), (33, 7861)]:
j = 1./jInv
print "j = %f s = %i" % (j,s)
print "k = %.17f +/- %e" % bisect(delta, 1-delta, delta,
lambda k: f(k, s, j))
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se