Advertisement
Guest User

HO3 Bisection

a guest
Sep 28th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. x = 25
  2. low = 0
  3. high = x
  4. epsilon = .01
  5. increment = 0.001
  6. ans = (low + high)/2.0
  7. guesses = 0
  8.  
  9. while abs(ans*ans - x) >= epsilon:
  10.     if ans*ans > x:
  11.         high = ans
  12.     else:
  13.         low = ans
  14.     guesses += 1
  15.     ans = (high + low)/2.0
  16. print guesses
  17. if abs(ans*ans - x) < epsilon:
  18.     print "The square root of ", x, "within a margin of ", epsilon, " is ", ans
  19. else:
  20.     print "no square root"
  21.  
  22. # 1. 12.25*12.25 > x. Low = 0, High = 25, ans =(0+25)/2 = 12.25
  23. # 2. 6.25*6.25 > x. Low = 0, High = 12.25, (0 + 12.5)/2= 6.25
  24. # 3. 3.125*3.125 < x. low =0, High = 6.25, ans = (0, 6.25)/2 = 3.125
  25. # 4. low = ans 3.125, High = 6.25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement