0

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

duplode
  • 31,361
  • 7
  • 69
  • 130
bbnn
  • 3,114
  • 8
  • 46
  • 64

2 Answers2

1

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... ;)

William
  • 997
  • 6
  • 11
0

Pseudocode is here, you should be able to figure it out from there.

Rafe Kettler
  • 69,672
  • 18
  • 145
  • 147