Approximating infinite sums is an important application of mathematics to engineering analysis. Here, you will write a function that keeps adding terms to a sum until the specified tolerance threshold is met.
Consider the sequence
3 17
1, \frac{3}{2}, \frac{17}{12}, ...
defined by
$x_1 = 1$, $x_k = \frac{1}{2}(x_{k-1} + \frac{2}{x_{k-1}})$ for $k = 2, 3, 4, ..., N$
The sequence converges on $\sqrt{2}$ as $N$ increases. The relative error, $\epsilon$, between the $N^{th}$ term in the sequence and $\sqrt{2}$ is given by the formula:
$\epsilon = |\frac{\sqrt{2} - x_N}{\sqrt{2}}|$
Note the vertical bars indicate an absolute value. Write a function called sqrt2approx that takes some user-specified value of the relative error threshold as the single input and determines the number of terms required to converge to this error. Your function should:
\begin{itemize}
\item Use a while loop to generate sequence terms until the relative error, $\epsilon$, is less than or equal to the user-specific error tolerance. Note that the user-specified error tolerance is the input to the function, not something to be defined within the function.
\item The first output of your function should be the value of the the last term generated.
\item The second output of your function should be the number of terms needed to meet the specified error tolerance.
\item Your function should also break the while loop if the number of terms needed to converge to the user-specified error tolerance exceeds 10 (this sequence converges really fast, hence the small upper limit on the number of terms). When this happens, set both the first and second output of the function to 0.
\end{itemize}