Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Figure 3.4 is below AND THE CODE HAS TO BE IN PHYTON!!! x = int(input('Enter an integer: ')) epsilon = 0.01 numGuesses = 0 low

Figure 3.4 is below AND THE CODE HAS TO BE IN PHYTON!!!

x = int(input('Enter an integer: ')) epsilon = 0.01 numGuesses = 0 low = 0.0 high = max(-5.0, x) ans = (high + low) / 2.0 while abs(ans**2 - x) >= epsilon: print ('low =', low, 'high =' , high, 'ans =', ans) numGuesses += 1 if ans**2 < x: low = ans else: high = ans ans = (high + low)/2.0 print('numGuesses =', numGuesses) print(ans, 'is close to square root of',x)

QUESTION - The code in Guttag's Figure 3.4 implements the bisection method to find the square root of a number. The bisection method is a very general method for finding the roots of polynomials, and for comparing two numbers.For example, the code in Figure 3.4 compares two values, to discover when ans^2 is close enough--that is, within epsilon of--the value x (here, ans is the proposed square root of x). When ans^2 falls within epsilon of x, then we have a good approximation for the square root of x.

The bisection method is also very useful for finding the roots of polynomials because, if you know the interval inside which a root of a polynomial equation lies, then the bisection method is guaranteed to converge to the root in that interval. The method may converge slowly, but it will converge: the bisection method will find the root. It's unusual when one gets such an absolute, global guarantee of convergence from a numerical method. But the bisection method delivers that guarantee--provided you give the method an interval inside which a root can be found.

In this exercise you must modify the code in Figure 3.4 so that it finds the three roots of the following polynomial:

-x^3 + 4x^2 + x - 6 = 0

Note that a root is defined as the value for x such that when plugged into this equation, the computed value of the left hand side of the equation equals zero--that is, a root satisfies the above polynomial equation.

The above polynomial equation has three real roots. One of the roots lies between 0 and 2.5. Another root lies between -5.0 and 0. And the third root lies between 2.8 and 7.0.

Modify Guttag's Figure 3.4 python code so that it finds approximations to all three roots of the above polynomial equation. Each approximation should cause the left hand side of the above equation to come to within 0.01 of zero (that is, the absolute value your computed left hand should be less than 0.01). Your code should also keep track of the number of guesses required for your approximation to fall inside this specified epsilon. Finally, your code should output the final value for the approximation to the root, as well as the number of guesses required to find that approximation.

NOTE: You do not have to modify Figure 3.4 python code to find all three approximate roots simultaneously! Instead, just modify the code so that you can input the appropriate initial low and high values for the interval endpoints (you might want to use two separate input statements to do this) and then run the code to find the one root in the specified interval. Run your code three separate times, with three separate interval inputs, to find the three approximate roots.

NOTE: Python's built-in function for finding the absolute value of a number is called abs(). So, for example, abs(-1.0) will return the value 1.0. abs(6.0) will return the value 6.0.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions