I am asked to write a program to solve this equation ( x^3 + x -1 = 0 ) using fixed point iteration.
What is the algorithm for fixed point iteration? Is there any fixed point iteration code sample in Python? (not a function from any modules, but the code with algorithms)
Thank You
First, read this: Fixed point iteration:Applications
I chose Newton's Method.
Now if you'd like to learn about generator functions, you could define a generator function, and instance a generator object as 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 be any initial guess...
Then you can gain successively better approximations by calling:
approxAnswer.next()
see: PEP 255 or Classes (Generators) - Python v2.7 for more info on Generators
For example
approx1 = approxAnswer.next()
approx2 = approxAnswer.next()
Or better yet use a loop!
As for deciding when your approximation is good enough... ;)