Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, we will implement the squareroot function-Math. squareroot in

ID: 3782922 • Letter: I

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 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|

Explanation / Answer

def sqrtStep(c: Double, xn: Double): Double = {
    return xn - (((xn*xn) - c)/(2*xn))
}

def sqrtN(c: Double, x0: Double, n: Int): Double = {
    if (n < 0) { throw new IllegalArgumentException }
    else if (x0 < 0) { throw new IllegalArgumentException }
    else if (n == 0) {return x0}
    else {
      sqrtN(c, sqrtStep(c,x0), n-1)
      var p = n
      var xp = x0
      while ( p > 0){
        p -= 1
        if (xp < 0) { throw new IllegalArgumentException }
        else {xp = xp - ((xp*xp)-c)/(2*xp)}
      }
      xp
    }
}

def sqrtErr(c: Double, x0: Double, epsilon: Double): Double = {
    if (x0 < 0) { throw new IllegalArgumentException }
    else if (epsilon <= 0) { throw new IllegalArgumentException }
    else if (abs((x0*x0)-c) < epsilon) {return x0}
    else {
     sqrtErr(c, sqrtStep(c, x0), epsilon)
     var xp = x0
     var cp = c
     var ep = epsilon
     while (abs((xp*xp)-cp) > ep){
      if (xp < 0) { throw new IllegalArgumentException }
      else { xp = xp - ((xp*xp)- cp)/(2*xp) }
     }
     xp
      }
}

def sqrt(c: Double): Double = sqrtErr(c, 1.0, 0.0001)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote