A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

can anyone help me run this code properly? when i test it, the code spits out obserd awnsers such as sqrtbi(9, 0.0001) = 2.25... instead of 3, how can i count on it to find the square root of any given number?

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    def sqrtbi(x, z): assert x>=0, 'x must be non-negitive, not' + str(x) assert z > 0, 'espilon must be positive, not' + str(z) low = 0 high = max(x, 1.0) guess = (low + high)/2.0 ctr = 1 while abs(guess**2 - x) > z and ctr <= 100: #print 'low:', low, 'high:', high, 'guess:', guess if guess**2 < x: low = guess else: high = guess guess = (low + high)/2.0 ctr += 1 assert ctr <= 100, 'Ineration count exceeded' print 'Bi method. Num. Interactios', ctr, 'Estimation', guess return guess

  2. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    sqrtbi(9.0, 0.01): Bi method. Num. Interactios 11 Estimation 3.00146484375 And maybe I'm a little psychotic, but I say it one more time, please use a pastebin to make it easy for us to copy the code. And no need to start a new question, you'd better take into account the answers given to the other one.

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    1 Attachment
  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you got it dude

  5. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    http://en.wikipedia.org/wiki/Pastebin Thx for the code, but because I'm a good guy, I reformatted it myself. And tested it too: sqrtbi(9.0, 0.00001) -> Bi method. Num. Interactios 21 Estimation 3.00000143051

  6. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    lol i understand now... I reformatted your code too well sorry. If you look at the return statement, it is inside the while loop. It should be after the loop, like this: http://pastebin.com/8H6YW4Kn

  7. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    haha THANK YOU!!!!!

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.