advertisement

6.006 Intro to Algorithms Recitation 12 October 26, 2011

Find root of f ( x ) = 0 through successive approximation e.g., f ( x ) = x

2

− a x i x i+1 y = f(x)

Figure 1 :

Newton’s Method.

Tangent at ( x i

, f ( x i

)) is line y = f ( x i

) + f

0

( x i

)

·

( x

− x i

) where f

0

( x i

) is the derivative.

x i +1

= intercept on x-axis x i +1

= x i

− f ( x i

) f

0

( x i

)

We want to find the square root of a number

Newton’s Method, so that our answer x a through successive approximation using will converge on a

1 / 2

. It looks like f ( x ) = a

1 / 2

− x has a root at x = a

1 / 2

. Let’s try to use it with Newton’s Method.

f ( x ) = f

0

( x ) =

χ i +1

=

=

=

= a

1 / 2

− x

−

1

χ i

− f ( χ i

) f

0

( χ i

) a

1 / 2

−

χ

χ i

−

χ i

+ a

1 / 2

−

1

−

χ i a

1 / 2

The choice of this function was unsuccessful, since we’re unable to compute

Let’s try squaring both sides of x = x i +1 using x i

.

a

1 / 2

. This time, it looks like we get something usable:

1

6.006 Intro to Algorithms Recitation 12 October 26, 2011 f ( x ) = x

2

− a

χ i +1

= χ i

−

( χ i

2

− a )

2 χ i

= a

χ i

+

χ i

2

Suppose

X n

=

√ a

·

(1 + n

) n may be + or -

Then,

X n +1

=

=

=

=

=

X n

+ a/X n

√

2 a (1 + n

) +

√ a a (1+ n

)

2 p

( a )

(1 + n

) +

1

(1+ n

) p p

(

( a a

)

)

2 + 2 n

2

+ n

2

2(1 + n

2 n

)

1 +

2(1 + n

)

Therefore, n +1

= n

2

2(1 + n

)

Quadratic convergence, as ] correct digits doubles each step.

We apply a first level of Newton’s method to solve f ( x ) = x

2

− a

. Each iteration of this first level requires a division. If we set the precision to d digits right from the beginning, then convergence at the first level will require lg d iterations. This means the complexity of computing a square root will be Θ( d

α lg d ) if the complexity of multiplication is Θ( d

α

) , given that we have shown that the complexity of division is the same as the complexity of multiplication.

However, we can do better, if we recognize that the number of digits of precision we need at beginning of the first level of Newton’s method starts out small and then grows. If the complexity of a d -digit division is Θ( d

α

) , then a similar summation to the one above tells us that the complexity of computing square roots is Θ( d

α

) .

2

6.006 Intro to Algorithms Recitation 12 October 26, 2011

χ i

+ b a/χ i c

Iteration:

χ i +1

= b c

2

Do floors hurt? Does program terminate? ( α and β are the fractional parts below.)

Iteration is

χ i +1

=

=

χ i

+

χ i

+ a

χ i

2 a

χ i

2

−

α

−

β

−

γ where γ =

α

2

+ β and 0

≤

γ < 1

Since a + b

≥

√ ab,

χ i

2 won’t stay stuck above if

+

2 i a

χ i

< 1

≥

√ a

, so subtracting

γ

(good initial guess).

always leaves us

≥ b

√ a c

. This

Now that we’ve seen square roots, let’s calculate cube roots. This time, x a

1 / 3

, instead. We can use f ( x ) = a

− x

3

: will converge on f ( x ) = x

3 f

0

( x ) = 3 x

2

− a

χ i +1

=

=

=

χ i

− f ( χ i

) f

0

( χ i

)

χ i

−

1

3

χ i

− a

χ

2 i

2

3

χ i

+

1 a

3 χ

2 i

3

6.006 Intro to Algorithms Recitation 12 October 26, 2011

Suppose x i +1

Then,

= a

1 / 3

·

(1 + i

) i may be + or -

χ i +1

=

=

=

=

=

=

=

2

3

χ i

+

1 a

3 χ

2 i

2 a

3

1 / 3

(1 + i

) +

1 a

3 ( a

1 / 3

(1 + n

))

2 a

1 / 3

3

2(1 + i

) +

(1 +

1 i

)

2 a

1 / 3

1

2(1 + 3

3 (1 + i

)

2 a

1 / 3

3

3 + 6 i

+ 6

1 + 2 i

2 i

+ 2

+

2 i a

1 / 3

3 +

3

2 i

+ 2

3 i

3 1 + 2 i

+

2 i

3 i a

1 / 3

1 +

2 i

+

(1 +

2

3

3 i i

)

2 i

+ 3

2 i

+

3 i

) + 1

Notice that the cubic term gets dominated by the quadratic term in the numerator, and that the denominator is very close to 1. Therefore, we have quadratic convergence here, just like in the square root case. The running time analysis is very similar to the square root case as well: the only difference is one additional multiplication in the denominator, so the running time stays the same as the running time of a multiplication.

4

MIT OpenCourseWare http://ocw.mit.edu

Fall 201 1

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms .