#!/usr/bin/env python # coding: utf-8 # # Example of fixed point iteration # Consider finding roots of # $$ # f(x) = x^2 - a, \qquad a > 0 # $$ # using following fixed point maps # \begin{eqnarray*} # x &=& x + c(x^2 - a) \\ # x &=& \frac{a}{x} \\ # x &=& \frac{1}{2}\left( x + \frac{a}{x} \right) # \end{eqnarray*} # In[3]: a = 3.0 c = 0.1 x1, x2, x3 = 2.0, 2.0, 2.0 print("%6d %18.10e %18.10e %18.10e" % (0,x1,x2,x3)) for i in range(10): x1 = x1 + c*(x1**2 - a) x2 = a/x2 x3 = 0.5*(x3 + a/x3) print("%6d %18.10e %18.10e %18.10e" % (i+1,x1,x2,x3)) # The first iteration diverges, the second oscillates while the last (Newton method) converges.