Question: In this exercise, we will implement the squareroot function-Math. squareroot in the JavaScript standard library. To do so, we will use Newton's method (also known


In this exercise, we will implement the squareroot function-Math. squareroot in the JavaScript standard library. To do so, we will use Newton's method (also known as Newton-Raphson). Recall from Calculus that a root of a differentiable function can be iteratively approximated by following tangent lines. More precisely, let f be a differentiable function, and let x_0 be an initial guess for a root of f. Then, Newton's method specifies a sequence of approximations x_0, x_1, ... with the following recursive equation:^1 x_n + 1 = x_n - f(x_n)/f'(x_n). The squareroot of a real number c for c > 0, written Squareroot c, is a positive x such that x^2 = c. Thus, to compute the squareroot of a number c, we want to find the positive root of the function: f(x) = x^2 - c. Thus, the following recursive equation defines a sequence of approximations for Squareroot c: x_n + 1 = x_n - x_n^2 - c/2x_n First, implement a function squareroot Step def squareroot Step(c: Double, xn: Double): Double that takes one step of approximation in computing Squareroot c (i.e., computes x_n+1 from x_n). Instructor Solution: 1 line. Next, implement a function squareroot N def squareroot N(c: Double, x0: Double, n: Int): Double that computes the nth approximation x_n from an initial guess x_0. You will want to call squareroot Step implemented in the previous part. Challenge yourself to implement this function using recursion and no mutable variables (i.e., vars)-you will want to use a recursive helper function. It is also quite informative to compare your recursive solution with one using a while loop. Instructor Solution: 7 lines {including 2 lines for closing braces and 1 line for a require). Now, implement a function squareroot Err def squareroot Err(c: Double, x0: Double, epsilon: Double): Double that is very similar to squareroot N hut instead computes approximations x_n until the approximation error is within E (epsilon), that is, |x_n^2 - c|
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
