Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

tcc8m

I am stuck on hand out 3 on an example using bisection search to find the square root of x (x=25). My issue: On the fourth iteration of the problem with x =25, low = 0, high = 6.25, ans = 3.125. 3.125*3.125 = 9.76 which is < 25. So now low = 3.125 and high =6.25. In the problem if ans*ans < x, low = ans. if ans*ans > x, high = ans. If both low and high map to same ans, how is it that in iteration 4 above that low = 3.125 and high = 6.25? More basic, why is there a local and global variable that are the same? Thanks! http://pastebin.com/i9BysyyQ

  • one year ago
  • one year ago

  • This Question is Closed
  1. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    Here is what I got after running the program sage: load "sqrt.sage" 1 2 3 4 5 6 7 8 9 10 11 12 13 The square root of 25 within a margin of 0.0100000000000000 is 5.00030517578125

    • one year ago
  2. tcc8m
    Best Response
    You've already chosen the best response.
    Medals 0

    Phi thank you for the thorough answer but I am still confused. In line 6 there is the variable ans= (high + low)/2 and in line 15 there is the same variable. If you change the low value from 0 to ans = 3.125 because 3.125*3.125 < 25 then doesn't that change the high value to 3.125 as well since both variables point to the same variable ans? I know that 6.25 * 6.25 > 25 which means that high takes the value of ans = 6.25. At this point forward does high actually = 6.25. or does high = ans. I am confused at how the variables high and low map to ans? I appreciate your help!

    • one year ago
  3. tcc8m
    Best Response
    You've already chosen the best response.
    Medals 0

    I think I understand now. So in round one high = 25, low = 0. ans = (0 +25)/2 = 12.25. 12.25*12.25 > 25 and so now high = the number 12.25 that ans at one point was equal to. I think I know what you mean. Would this be a good simple example?: If a=9 and b= a a = 10 b = 9 then right?

    • one year ago
  4. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    I looked into more details on python, and I am leading you astray a bit See http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html#issues-with-mutable section 2.4.7. More on Mutable and Immutable Types Python does use pointers as you suggest, but it acts as if it does not when dealing with immutable types such as int or str. in python, when you say high= ans both ans and high are pointing to the same number but when you change ans with ans= (high+low)/2, it allocates a new location for ans, and gives this new location the new value.

    • one year ago
  5. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    to answer your question If a=9 and b= a at this point, both a and b "point" to a location with 9 in it. a = 10 This causes a to point to a different location with 10 in it b will continue to point to the location with 9 in it b = 9 I think this causes b to point to a new location with 9 in it...

    • one year ago
  6. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    So I should revise my previous answer when you ask if both low and high map to same ans... once you compute a new number, python allocates a new memory location, and low (or high) points to the new location. So the "mapping" does not last

    • one year ago
  7. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    you asked high takes the value of ans = 6.25. At this point forward does high actually = 6.25. or does high = ans. high = ans. But when you execute the line ans = (high + low)/2.0 ans points to a different location with this new value. so though it is wrong to say high and ans are always different, python makes it look like they are always different variables with their own memory locations.

    • one year ago
  8. tcc8m
    Best Response
    You've already chosen the best response.
    Medals 0

    ok that makes it clear. Thank you for helping me through it. I feel much better now. Also, thank you for the link. I also found another good link that explains the concept with pictures. http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables

    • one year ago
  9. phi
    Best Response
    You've already chosen the best response.
    Medals 1

    ok, glad I didn't lead you to far off ... I don't know python, so I jumped to conclusions...all these languages have the same underlying ideas, but the details do vary.

    • one year ago
    • Attachments:

See more questions >>>

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.