6 - 1/25/2010 Problem Find the square root of N (i.e. solve...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1/25/2010 1 Problem Find the square root of N (i.e. solve x 2 = N ) One possible approach is the Babylonian Method for finding square roots. First we rearrange the equation as follows: x N x x x N x x x N x x x N x x N x N x 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 Then we pick an arbitrary first guess ) and repeatedly refine it by applying: Then we pick an arbitrary first guess ( x 0 i i i x N x x 2 1 1 function [ x ] = Babylon( N ) %BABYLON Uses Babylonian technique to find the square root of a value. % Inputs: N = value of interest % Outputs: x = square root of N xold = N; f k 1 20 for k = 1 : 20 x = 0.5*(xold + N/xold); change = abs(x xold); fprintf ('x(%d) = %f, absolute change = %f\n', k, x, change ); if change < 1e 6 fprintf ('Babylonian technique has converged.\n'); return; end xold = x; %get ready for next iterations xold x; %get ready for next iterations end; error ('Babylonian technique has not converged.'); end
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1/25/2010 2 When x 0 = N the approach rapidly converges on the solution: >> Babylon (7) x(1) = 4.00000000, absolute change = 3.00000000 x(2) = 2.87500000, absolute change = 1.12500000 x(3) = 2.65489130, absolute change = 0.22010870 (4) 2 64576704bl t h 0 00912426 x(4) = 2.64576704, absolute change = 0.00912426 x(5) = 2.64575131, absolute change = 0.00001573 x(6) = 2.64575131, absolute change = 0.00000000 Babylonian technique has converged.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 04/14/2010.

Page1 / 7

6 - 1/25/2010 Problem Find the square root of N (i.e. solve...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online