Stats 102A Project 1: Newton's Method for the Square Root.

Author: Alen Lai

UID:203809385

Date: 10-21-13

Objective: The purpose of this project is to write a function that computes the square root of an arbitrary positive number. In our case, we will use the Newton's Method to approximate the square root of any non-negative integer or fraction.

R-code is listed below:

mySquareRoot <- function(Xinitial, f, eps = 1e-06, itmax = 100, verbose = TRUE, 
    ...) { # two iterates are equal at 1e-06, 100 maximum iterations, outputs all the iterations 
    oldx <- Xinitial # For the 1st iteration, oldx is set to the value of Xinitial 
    itnum <- 1 # denotes the 1st iteration entry 
    repeat {   # repeats iterations until guess does not change anymore 
        newx <- f(oldx, ...)
        if (verbose) 
            cat("Iteration: ", formatC(itnum, width = 1, format = "d"), "oldx:", 
                formatC(oldx, digits = 5, width = 8, format = "f"),"newx:", # each value of old x is rounded to 5 digits after decimal point, width between columns is set to 8
                formatC(newx, digits = 5, width = 8, format = "f"), "\n") # each value of new x is rounded to 5 digits after decimal point, width between columns is set to 8  
        if ((sucitrDist(oldx, newx) < eps) || (itnum == itmax)) { 
            return(newx) # returns the new value of x 
        }
        oldx <- newx # After 1st iteration, old x is set to the value of newx from the previous iteration 
        itnum <- itnum + 1 # the nth entry after each successive iteration 
    }
}

For this project, we are going to compute the square root of 37, 0.85, and 91.

First, lets compute the square root of 37.

sucitrDist <- function(x, y) return(max(abs(x - y)))  # function which measures distance between successive iterations 
fSqrt <- function(x, y) (x + (y/x))/2  # iterating function -> Newton's Method 
sqrty1 <- mySquareRoot(2, fSqrt, eps = 1e-06, itmax = 100, verbose = TRUE, y = 37)  # computes the square root of 37
## Iteration:  1 oldx:  2.00000 newx: 10.25000 
## Iteration:  2 oldx: 10.25000 newx:  6.92988 
## Iteration:  3 oldx:  6.92988 newx:  6.13454 
## Iteration:  4 oldx:  6.13454 newx:  6.08298 
## Iteration:  5 oldx:  6.08298 newx:  6.08276 
## Iteration:  6 oldx:  6.08276 newx:  6.08276

Second, lets compute the square root of 0.85

sqrty2 <- mySquareRoot(2, fSqrt, eps = 1e-06, itmax = 100, verbose = TRUE, y = 0.85)  # computes the square root of 0.85 
## Iteration:  1 oldx:  2.00000 newx:  1.21250 
## Iteration:  2 oldx:  1.21250 newx:  0.95677 
## Iteration:  3 oldx:  0.95677 newx:  0.92259 
## Iteration:  4 oldx:  0.92259 newx:  0.92195 
## Iteration:  5 oldx:  0.92195 newx:  0.92195

Finally, lets compute the square root of 91.

sqrty3 <- mySquareRoot(2, fSqrt, eps = 1e-06, itmax = 100, verbose = TRUE, y = 91)  # computes the square root of 91 
## Iteration:  1 oldx:  2.00000 newx: 23.75000 
## Iteration:  2 oldx: 23.75000 newx: 13.79079 
## Iteration:  3 oldx: 13.79079 newx: 10.19470 
## Iteration:  4 oldx: 10.19470 newx:  9.56045 
## Iteration:  5 oldx:  9.56045 newx:  9.53942 
## Iteration:  6 oldx:  9.53942 newx:  9.53939 
## Iteration:  7 oldx:  9.53939 newx:  9.53939