In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 2nd
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #04 exercise sheet]
In this problem you should submit a function as described. Inside the function do not print anything that was not asked!
A numeric method to determine an approximate value for the square root of a number x consists of iteratively computing n estimates according to the following equation:
\[
y_n = \frac{1}{2}\Big(y_{n-1} + \frac{x}{y_{n-1}}\Big)
\]
The initial estimate, \( y_0 \) is a rough value for the square root (e.g.,\( \frac{x}{2} \) ). At each iteration, the error of the estimate is given by the diference between the last two values calculated, that is \( |y_n - y_{n-1}| \). The approximation ends when the allowed error (i.e., a reasonable error threshold) is achieved.
Write a function approximate_sqrt(x, max_error) that approximates the square root of a given number x using this iterative method, starting with \( y_0 = \frac{x}{2} \). The approximation should stop when the error is less or equal to max_error. The estimated value should be return by the function.
The following limits are guaranteed in all the test cases that will be given to your program:
0 < x ≤ 109 | The number for which you will approximate the square root | |
0 < max_error < 1 | The maximum allowed error for the approximation |
Example Function Calls | Example Output |
print( round(approximate_sqrt(2, 0.1), 16) ) print( round(approximate_sqrt(2, 0.05), 16) ) print( round(approximate_sqrt(2, 0.001), 16) ) print( round(approximate_sqrt(2, 0.000001), 16) ) |
1.4166666666666665 1.4142156862745097 1.4142135623746899 1.414213562373095 |