Vous êtes sur la page 1sur 4

Root Approximation.

xmcd 5/26/2008
MathCad - ver. 13 MathCad - ver. 13 MathCad - ver. 13 MathCad - ver. 13
by Lothar Uher May, 2008
Root Approximation (Newton) Root Approximation (Newton) Root Approximation (Newton) Root Approximation (Newton)
1. Square Root Approximation:
Newton's method is a common algorithm to approximate a square root of X. It is computed by
iteration. A result is repeatedly calculated with the previous value until the difference between
the squared result and X is better than the tolerance value. The number of digits doubles roughly
with each iteration. The computation result is shown below using MathCADs root function and the
"sqrt" function.
Reference: Square Root Approximation (From Wikipedia, the free encyclopedia)
Test Data for Root Approximation
Tolerance value for iterative computations:
tol 0.000000001 :=
Value definition for the root computations:
X 9367.2345 :=
Square Root Implementation
sqrt x , ( )
x
4

i 0

1
2


i i 1 +

2
x while

return
:=
X 96.78447447808972 =
sqrt X tol , ( )
96.78447447808972
9

=
Square Root returned
1) square root of X
2) number of iterations
1 of 4
Root Approximation.xmcd 5/26/2008
2. N
th
Root Approximation:
The square root algorithm above is a special case of the n
th
root algorithm which converges very
fast. For large n the algorithm is less efficient due to an exponentiation. It can be improved with
an efficient power algorithm (which is shown below). It is again computed repetitively by using the
previous result until the difference of the power result and X or the difference between the
previous and current computed result is better than the tolerance value . The computation
result is shown below using MathCADs root functions and the "nart" function.
Reference: Nth Root Approximation (From Wikipedia, the free encyclopedia)
nart x , , ( )
x
2

0
1
i 0
i i 1 +

while

return
:=
4
X 9.83791006657866 =
X
1
4
9.83791006657866 =
nart X 4 , tol , ( )
9.837910066578658
22

=
n
th
Root returned (n=4)
1) n
th
root of X
2) number of iterations
3. Power Functions - Exponentiation by Squaring, etc.:
To take an integer power one could simply multiply a number x as many times by itself as the
exponent requires which is called long multiplication. It uses O(n) multiplications and is less
efficient then using the algorithm called exponentiation by squaring. The information provided can
be found in Wikipedia, the free encyclopedia. When we compare this algorithm to the long
2 of 4
Root Approximation.xmcd 5/26/2008
be found in Wikipedia, the free encyclopedia. When we compare this algorithm to the long
multiplication method, we find that exponentiation by squaring uses O(log n) multiplications. Its
execution time is much faster than the long multiplication. The speed improvement can be
compared in a way to long multiplication versus addition method, which speeds up multiplication
over the slower method of repeated addition. The benefit of the power algorithm by squaring
takes effect for exponents greater than or equal to 4. In the same way as above the algorithm
was implemented in MathCAD (see routines below) and was tested with an example value for X and
exponent. The computation results are shown below using MathCADs power function and the
"*pow" functions.
Reference: Exponentiation by Squaring (From Wikipedia, the free encyclopedia)
Long Multiplication
Test Data for Power Functions and MathCAD Result
B 5 := n 21 := B
n
476837158203125 =
Exponentiation by Squaring Implementation
epow x , ( ) 1
i 0
x
1
mod 2 , ( ) 0 if
x x
2

otherwise
i i 1 +
0 > while

return
:=
epow B n , ( )
476837158203125
7

=
Exponentiation by Squaring
n
th
power of B 1.
number of iterations 2.
3 of 4
Root Approximation.xmcd 5/26/2008
Long Multiplication Implementation
mpow x , ( ) 1
i 0
x
i i 1 +
1
0 > while

return
:=
mpow B n , ( )
476837158203125
21

=
Long Multiplication
n
th
power of B 1.
number of iterations 2.
Multiplication by Addition Implementation
apow x , ( ) i 0
x
0
j x
+
j j 1
i i 1 +
j 0 > while

1
1 ( ) 0 > while

return
:=
apow B n , ( )
476837158203125
100

=
Multiplication by Addition
n
th
power of B 1.
number of iterations 2.
4 of 4

Vous aimerez peut-être aussi