What if your computer didn\'t have a built-in function to compute square roots?
ID: 3888055 • Letter: W
Question
What if your computer didn't have a built-in function to compute square roots? Could you program a function which computes it using only basic arithmetic operations? The problem: given X > 0, find x > 0, such that Program all three methods below and compare their convergence rates. Try X = 0.01, 4, 1000 Bisection. You must first transform this problem into a problem of finding roots of some equation, f(x) 0. (Don't use the square root function!) Here is one possibility: f(x) -x2 - X; can you think of any other? To set up the bisection algorithm, you must first find an initial interval [a, b] which contains VX. Hint: you may use that X s VX for X s 1 and VX s Xfor X 21. Newton's method. Show that Newton's method (with f(x) as above) amounts to iterating n+1 2 1 What will you pick as the initial guess, xo? Functional Iterations. Here you need to transform the relation, X-X into x = f(x) (again, without explicitly using the square root function). Here is one way to do this Is this relation adequate to compute VX for any X? More generally, we can set up the functional iterations, e.g., via (1-a)x + X/x, # 0 is an arbitrary number. Do you spot similarity with Newton's method? Is there a way to pick the optimal value of the parameter, a? Try = 14, 112, 314 Comparison. Compute v2 using these three methods up to some given precision, i.e., stop the iter- ations when the difference between the two most recent approximations is less than 1013 (or some other small number). Compare the number of iterations needed to achieve the chosen precision in these methodsExplanation / Answer
(1)# Python Program - Find the square root of a number without using inbuilt function
while True:
print("Enter 'x' for exit.")
num= input("Enter a number : ")
if num == 'x' :
break
else:
number= float(num)
number_sqrt= number * * 0.5
print("Square root of %0.2f is %0.2f " %(number,number_sqrt))
The above program uses simple airthmatic operator ** exponent avaiable in python.This program is in infinite loop (while True)which breaks only when user enter 'x' to exit from program.Next statement num= input("Enter a number : ") will accept input from the user which will be stored in x.This statement if num == 'x' : which check whether user has pressed 'x' or not, if user pressed 'x' then loop will break and program exits, otherwise statement : number= float(num) will first convert integer value into float value so that if we find square root of non - square numbers then aso it will give accurate result. number_sqrt= number * * 0.5 This statement will find number to the power of 0.5 i.e (x)0.5 which is actually the square root of a number. print("Square root of %0.2f is %0.2f " %(number,number_sqrt)) will print the square root of number. %0.2f will format result upto two decimal places.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.