Question

In: Computer Science

a) you can see a program for using bisection search to find the square root of...

a) you can see a program for using bisection search to find the square root of x:

x = 25

epsilon = 0.01

low = 0.0

high = max(1.0, x)

guess = (low + high) / 2

numberofguesses = 1

while abs(guess ** 2 - x) > epsilon :

print('low =', low, 'high = ', high, 'guess = ', guess)

if guess** 2 > x : # the guess is too high, so move high down to guess

high = guess

else : # the guess is too low, so move low up to guess

low = guess

print('number of guesses:', numberofguesses)

print(guess, 'is close enough to the square root of ', x)

Try out the program, and fix any typos (mine or yours) so that it works. Think about why it starts high with max(1, x) in place of simply high = x. Change the program so that the value of x is read from the user. Ask the user for a positive number. If the user gives a negative number, then ask again, repeatedly until a positive number is given.

b) Modify the bisection search program shown above, so that it finds the cube root of a number. The number can be negative or positive.

Solutions

Expert Solution

The corrected program is as follows:

while True:
    inp = float(input("Calculate square root of? "))
    if inp<=0:
        print('Please enter a positive number')
        continue
    else:
        break
x = inp
epsilon = 0.01
low = 0.0
high = max(1.0, x)
guess = (low + high) / 2
numberofguesses = 1
while abs(guess ** 2 - x) > epsilon :
    print('low =', low, 'high = ', high, 'guess = ', guess)
    numberofguesses+= 1
    if guess** 2 > x : # the guess is too high, so move high down to guess
        high = guess
    else : # the guess is too low, so move low up to guess
        low = guess
    guess = (low + high) / 2
print('number of guesses:', numberofguesses)
print(guess, 'is close enough to the square root of ', x)

Reason we use high=max(1.0,x)

When x is between 0 and 1, the square root of x is greater than x itself. Therefore we can't just set high to x, because it needs to start out higher than the desired answer.

Bisection method to find cube root:

n = float(input())
if (n>0 and n<1) or (n>-1 and n<0):
    low=abs(n)
    high=1
else:
    if n>=1 or n<=-1:
        low=0
        high=abs(n)
eps = 0.0000001
numberOfGuesses = 0
mid = (low + high) / 2.0
while abs((mid**3)-abs(n)) > eps :
        if mid**3 < abs(n):
                low = mid
        else:
                high = mid
        mid = (low + high)/2.0
        numberOfGuesses = numberOfGuesses + 1
guess = mid
if n>0:
    print ("Cube Root of ",n ," : ", guess, " in ",numberOfGuesses," guesses.")
else:
    print ("Cube Root of ",n ," : ", -1*guess, " in ",numberOfGuesses," guesses.")
    

Related Solutions

USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE...
USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE INTERVAL [ 0 , 1 ] UP TO 3 DECIMAL PLACES. USE NEWTON'S METHOD TO APPROXIMATE THE ROOT OF f(x)=x^2-5    IN THE INTERVAL  [ 2 , 3 ] UP TO 4 DECIMAL PLACES.
I am asked to find the square roots using the bisection method for x * x...
I am asked to find the square roots using the bisection method for x * x - a = 0. I was wondering how the bisection method is performed. Let's suppose a = 9, so I would need to find the roots of x * x - 9 = 0. Also, from the 1st equation, when would the bisection method NOT output a root?
To find a positive root for , write a MATLAB script file that uses Bisection method....
To find a positive root for , write a MATLAB script file that uses Bisection method. Choose any initial value that is needed. Use absolute relative approximate error to be less than 0.01. Your code should report the number of iteration and the value of x.
How far can you go with transformations? For instance, can I transform data using square root,...
How far can you go with transformations? For instance, can I transform data using square root, arcsin, and then natural log? If I shouldn’t, why not?
use bisection to find the real root x = sin(x) + 1 with the initial guesses...
use bisection to find the real root x = sin(x) + 1 with the initial guesses of x l = 0 and x u = 3. perform the computation until the approximate error falls below 5%
find the polar presentation of (j-square root of 3)
find the polar presentation of (j-square root of 3)
Can you confirm, within measurement errors, that the period is proportional to the square root of...
Can you confirm, within measurement errors, that the period is proportional to the square root of the length ` of pendulum, and that it is independent of the mass m of the bob? Explain what frequency and period mean in your own words. Also, think about conservation of mechanical energy during the oscillation: when is the kinetic energy maximal, when is it minimum? How about the potential energy? Explain how “conservation of mechanical energy” is manifest in oscillation of simple...
Using the bisection method:     Make a program to use this method using the following three...
Using the bisection method:     Make a program to use this method using the following three functions and use it to find the root of this function f (x) = x * x * x-8. a) A function so that the user between xlower and xupper that meets the value of the function has a different sign and if he does not ask for new values. b) A function to find the root and call it bisection and perform a...
Find the domain of the function g(x) = square root of 3x+3
Find the domain of the function g(x) = square root of 3x+3
you are required to search for TWO newspaper articles (national or international) where you can see...
you are required to search for TWO newspaper articles (national or international) where you can see some concept(s) of microeconomics being applied. Just a soft reminder for students, that one such article was made a part of your course pack as well (those who have read it will know which newspaper article I am referring to.) (Hint: it was related to Demand, Supply and Market Equilibrium).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT