# -*- coding: utf-8 -*- """ Created on Mon Aug 26 21:52:16 2013 This is homework0.py. It uses python to calculate sqrt(2) by solving x^2 = 2. We use the method detailed in class ( successive approximation). We solve x = f(x) where f(x) = (x + 2/x)/2. Start with x0 = 1.0. change initial values of x and see what happens. You may have to change the number of iterations also @author: dave semeraro """ from math import sqrt; x=1.0; gx = 0.0; numberofiterations = 5; for i in range(numberofiterations): gx = (x + 2.0/x)/2.0; x = gx; err = sqrt(2.0) - gx; relerr = err/sqrt(2.0); print '%3d %13e %13e' % (i, err, relerr);