Question: Note from me: This is using Python 3 coding. I've been working on this project for awhile and am getting no where. The more detail
Note from me: This is using Python 3 coding. I've been working on this project for awhile and am getting no where. The more detail you can give my in explaining your code the better!
Problem:
Approximate the square root of a number using an iterative algorithm; compare result of this algorithm to Python square root function result.
Your square root function should use the following equation to generate an approximate square root for n:
Xk+1 = (1/2) (Xk + (n/Xk) , where X0 = 1
Each value of x should be a better approximation for the square root of n.
Requirements
Your program should include three Python functions. mysqrt will compute the approximate square root for a given number. sqrt_compare will call m ysqrt and compare the value retur ned by mysqrtwith the square root returned by th e sqrt function from th e math module in t he P ython St andard Library. Finally, you will writ e and call a main function to execute your program. mysqrt will have two parameters: n, a positive integer to fi nd the square root of, and k, the n umber of times the iterative square root appr oximation process should run . The f unction should use the equation given above to determine the approximate square root of n, and return (note: not print) this result. mysqrt should return correct values for the simple e xamples included in your docstring, and also for the test cases given here:
>>> mysqrt(25, 5)
5.000023178253949
>>> mysqrt(25, 10)
5.0
>>> mysqrt(100, 10)
10.0
>>> mysqrt(625, 5)
29.1828724815876
>>> mysqrt(10000, 8)
101.20218365353946
>>> mysqrt(10000, 10)
100.00000025490743
>>> mysqrt(10000, 11)
100.0
sqrt_compare will have two parameters: num , a positive integer to find the square root of, and iterations , the number of times the iterative square root approximation process should run.
sqrt_compare will call mysqrt (with numand iterationsas arguments) and then compare the value returned by mysqrt to the square root of num returned by math module sqrt.
sqrt_compare should report on (print) the difference between the two values as a percentage error (the absolute value of math sqrt result - mysqrt result, divided by math sqrt result, then multiplied by 100) rounded to 2 decimal places. sqrt_compareshould return None.
sqrt_compareshould yield the following, for example:
>>> sqrt_compare(10000, 8)
For 10000 using 8 iterations:
mysqrt value is: 101.20218365353946
math lib sqrt value is: 100.0
This is a 1.2 percent error.
Finally, write a mainfunction. The mainfunction will not have any parameters, and will return None. For the mainfunction (ONLY), you do not need any documentation beyond a brief description. The mainfunction should call function sqrt_compare for each of the seven examples given above. Then, call function main to execute your program:
def main():
'''Square root comparison program driver.'''
sqrt_compare(25, 5)
sqrt_compare(25, 10)
sqrt_compare(625, 5)
sqrt_compare(625, 10)
sqrt_compare(10000, 8)
sqrt_compare(10000, 10)
sqrt_compare(10000, 11)
return None
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
