Java Reference
In-Depth Information
do
{block_instruction;}
while (boolean_expression);
That is, the
boolean expression
is evaluated after the block of instructions,
and not prior to its execution,
a
s it is the case for
while
structures. Consider
computing the square root
√
a
of a non-negative number
a
using Newton's
method. Newton's method finds the closest root to a given initial condition
x
0
of a smooth function by iterating the following process: Evaluate the tangent
equation of the function at
x
0
, and intersect this tangent line with the
x
-axis:
This gives the new value
x
1
. Repeat this process until the difference between
two successive steps go beyond a prescribed threshold (or alternatively, repeat
k
times this process). For a given value
x
n
, we thus find the next value
x
n
+1
by setting the
y
-ordinate of the tangent line at (
x
n
,f
(
x
n
)) to 0:
y
=
f
(
x
n
)(
x
−
x
n
)+
f
(
x
n
)=0
.
y
=
f
(
x
)
f
Tangent line
y
=
f
(
x
n
)(
x − x
n
)+
f
(
x
n
)
f
(
x
n
)
x
n
+2
x
n
+1
x
n
+1
=
x
n
−
x
n
x
f
(
x
)
f
(
x
n
)
Figure 2.3
Newton's method for finding the root of a function
It follows that we get:
f
(
x
)
f
(
x
n
)
.
Figure 2.3 illustrates this root finding process. Let us use Newton's method to
calculate the square root function of
a
by finding the root of equation
f
(
x
)=
x
2
x
n
+1
=
x
n
−
−
a
. We implement the loop using a
do
structure as follows:
Search WWH ::
Custom Search