python - fixed point iteration algorithm -
i asked write program solve equation ( x^3 + x -1 = 0 ) using fixed point iteration.
what algorithm fixed point iteration? there fixed point iteration code sample in python? (not function modules, code algorithms)
thank you
first, read this: fixed point iteration:applications
i chose newton's method.
now if you'd learn generator functions, define generator function, , instance generator object follows
def newtons_method(n): n = float(n) #force float arithmetic nplusone = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1) while 1: yield nplusone n = nplusone nplusone = n - (pow(n,3) + n - 1)/(3*pow(n,2) +1) approxanswer = newtons_method(1.0) #1.0 can initial guess...
then can gain successively better approximations calling:
approxanswer.next()
see: pep 255 or classes (generators) - python v2.7 more info on generators
for example
approx1 = approxanswer.next() approx2 = approxanswer.next()
or better yet use loop!
as deciding when approximation enough... ;)
Comments
Post a Comment