Square Root Newton-Raphson140 views

AlgoPill
 function squareRootNewtonRaphson(num){
    var sqroot = num/2, 
        epsilon = num/1000000, //10^(-6);
        error = sqroot*sqroot - num;
    while(Math.abs(error)>epsilon){
        // xn+1 = xn - (xn*xn - num)/(2*xn);
        sqroot = sqroot - (error)/(2*sqroot);
        error = sqroot*sqroot - num;
    }
    return sqroot;
}

Problem Statement: Given a number find its square root.

Solution:  is to use newton-raphson  method.

let input number be num and its square root be x

then x² = num

x² – num =0;

f(x) = 0

where f(x) = x² – num

according to newton-raphson

xn+1 = xn – f(x)/f’(x)

where xn+1 and xn are approximate roots for equation f(x)=0

and f’(x) is derivative of f(x) with respect to x

thus f’(x) = 2*x

According to this method gets xn+1 is more accurate than xn with each iteration

we run these iteration until error(x²-num) is within some bound epsilon

Post a Comment

You must be logged in to post a comment.