[Tutor] problems with the bisect method
Mic Forster
micforster@yahoo.com
Tue Mar 4 22:17:02 2003
Hi guys,
I am using the bisect method to find the value of x. I
am sure I am missing something simple here so I put it
to you. Thanks for any help.
>>> from math import log
>>> def f(x, s):
return ((1 - x) / x) * (-(log(1 - x)))
>>> j = 1. / 7861
>>> s = 33
>>> def bisect(min, max, delta, function):
fMax = function(max)
fMin = function(min)
assert fMax * fMin < 0
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 fun(k, s):
return f(k, s) - j
>>> delta = 1e-9
>>> bisect(delta, 1-delta, delta, fun)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in ?
bisect(delta, 1-delta, delta, fun)
File "<pyshell#5>", line 2, in bisect
fMax = function(max)
TypeError: fun() takes exactly 2 arguments (1 given)
>>>
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/