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?
Using the bisection method, find the root of the following function: f(x)=cos(2x) - x Use the...
Using the bisection method, find the root of the following function: f(x)=cos(2x) - x Use the following initial values: xl=0 and xu=2 NOTE: Perform only 3 iterations.
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?
1. Give a command using find to search from the root directory the file program.f and...
1. Give a command using find to search from the root directory the file program.f and redirect any errors to the file myerrors.txt 2. Give a command for finding files having the letters index as the beginning of the file name and located in your home directory (provide the absolute path) 3. Give a command for finding within a directory called /mp3collection, only those mp3 files that have a size less than 5000 Kilobytes (< 5MB). 4. Give a commmand...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT