2014年9月10日星期三

Mathematical Principles of Bisection Method for Finding Square Roots

Python code. (Bisection method for finding square roots)
def squareRootBi(x, epsilon):
    """Assume x>=0 and epsilon>0
       Return y s.t. y**2 is within epsilon of x"""
    assert x>=0, "x must be non-negative, not" + str(x)
    assert epsilon>0, "epsilon must be positive, not" + str(epsilon)
    low = 0
    high = max(x,1.0)
    guess = (low + high)/2.0
    counter = 1
    while abs(guess**2 - x) > epsilon and counter <= 100:
        if guess**2 < x:
            low = guess
        else:
            high = guess
        guess = (low + high)/2.0
        counter +=1
    assert counter <=100, "iteration count exceeded."
    print "Bi-method, Num iterations:", counter, "Estimate:", guess
    return guess

We assert that guess**2 tends to x as epsilon tends to 0. To prove this statement we need the following theorem named Nested Interval Theorem which is the fundamental theorem in mathematical analysis.

Theorem. (Nested Interval Theorem)
If I_n=[a_n,b_n] is a sequence of nonempty bounded intervals satisfying I_n>I_2>...>I_n>.... Moreover assuming that the length of I_n tends to 0 as n tends to infinity, then there exists a unique real number c belongs to all the intervals I_n, n=0,1,2,.... such that both of a_n and b_n tend to c as n tends to infinity.

Without loss of generalization, we assume that x>1 and epsilon=1/n. The above code yields a sequence of intervals: I_n, n=0,1,2,..., where we set I_0=[0,x], such that the conditions in the above theorem, that is I_0>I_1>I_2>...>I_n and length of I_n is x/2**n, n=0,1,2,.... Then there exists a unique real number y belongs to all the interval I_n, n=0,1,2,.... Now we can easily see that a_n**2 and b_n**2 tend to x, hence y**2=x. Indeed, we assume that I_n=[a_n, b_n], and from the code it follows that a_n**2<x<b_n**2, letting epsilon tend to 0, that is, n tends to infinity, we see that both of a_n**2 and b_n**2 tend to x, therefore y**2=x.


没有评论:

发表评论