牛顿迭代法与二分法计算平方根

``````/**
* 牛顿迭代法求平方根
* @param  number   求值的数
* @param  accuracy 精度
* @return          Double
*/
public static double NewtonSqrt(double number, double accuracy) {
//第一个猜测值
double guess = number / 2;
int count = 0;
if (number < 0) {
return Double.NaN;
}
//当两个猜测的差值大于精度即return
while (Math.abs(guess - (number / guess)) > accuracy) {
//迭代公式推导而成
guess = (guess + (number / guess)) / 2;
count++;
System.out.printf("try count = %d, guess = %f\n", count, guess);
}
System.out.printf("final result = %f\n", guess);
return guess;
}
``````

``````    public static double DichotomySqrt(double number, double accuracy) {
double higher = number;
double lower = 0.0;
double middle = (lower + higher) / 2;
double last_middle = 0.00;
int count = 0;
if (number < 0) {
return Double.NaN;
}
while (Math.abs(middle - last_middle) > accuracy) {
if (middle * middle > number) {
higher = middle;
} else {
lower = middle;
}
last_middle = middle;
middle = (lower + higher) / 2;
count++;
System.out.printf("Dichotomy try count = %d, guess = %f\n", count, last_middle);
}
System.out.printf("Dichotomy final result = %f\n", last_middle);
return last_middle;
}
``````

``````
public static void main(String[] args) {
double result = NewtonSqrt(2,1e-3);
double dichotomyRes = DichotomySqrt(2,1e-3);
}
``````

``````try count = 1 guess = 1.5
try count = 2 guess = 1.4166666666666665
try count = 3 guess = 1.4142156862745097
final result = 1.4142156862745097

Dichotomy try count = 1 guess = 1.0
Dichotomy try count = 2 guess = 1.5
Dichotomy try count = 3 guess = 1.25
Dichotomy try count = 4 guess = 1.375
Dichotomy try count = 5 guess = 1.4375
Dichotomy try count = 6 guess = 1.40625
Dichotomy try count = 7 guess = 1.421875
Dichotomy try count = 8 guess = 1.4140625
Dichotomy try count = 9 guess = 1.41796875
Dichotomy try count = 10 guess = 1.416015625
Dichotomy final result = 1.416015625
``````

``````
public static void main(String[] args) {
double result = NewtonSqrt(2,1e-15);
double dichotomyRes = DichotomySqrt(2,1e-15);
}
``````
``````
try count = 1 guess = 1.5
try count = 2 guess = 1.4166666666666665
try count = 3 guess = 1.4142156862745097
try count = 4 guess = 1.4142135623746899
try count = 5 guess = 1.414213562373095
final result = 1.414213562373095

Dichotomy try count = 1 guess = 1.0
Dichotomy try count = 2 guess = 1.5
Dichotomy try count = 3 guess = 1.25
Dichotomy try count = 4 guess = 1.375
Dichotomy try count = 5 guess = 1.4375
Dichotomy try count = 6 guess = 1.40625
Dichotomy try count = 7 guess = 1.421875
Dichotomy try count = 8 guess = 1.4140625
Dichotomy try count = 9 guess = 1.41796875
Dichotomy try count = 10 guess = 1.416015625
Dichotomy try count = 11 guess = 1.4150390625
Dichotomy try count = 12 guess = 1.41455078125
Dichotomy try count = 13 guess = 1.414306640625
Dichotomy try count = 14 guess = 1.4141845703125
Dichotomy try count = 15 guess = 1.41424560546875
Dichotomy try count = 16 guess = 1.414215087890625
Dichotomy try count = 17 guess = 1.4141998291015625
Dichotomy try count = 18 guess = 1.4142074584960938
Dichotomy try count = 19 guess = 1.4142112731933594
Dichotomy try count = 20 guess = 1.4142131805419922
Dichotomy try count = 21 guess = 1.4142141342163086
Dichotomy try count = 22 guess = 1.4142136573791504
Dichotomy try count = 23 guess = 1.4142134189605713
Dichotomy try count = 24 guess = 1.4142135381698608
Dichotomy try count = 25 guess = 1.4142135977745056
Dichotomy try count = 26 guess = 1.4142135679721832
Dichotomy try count = 27 guess = 1.414213553071022
Dichotomy try count = 28 guess = 1.4142135605216026
Dichotomy try count = 29 guess = 1.414213564246893
Dichotomy try count = 30 guess = 1.4142135623842478
Dichotomy try count = 31 guess = 1.4142135614529252
Dichotomy try count = 32 guess = 1.4142135619185865
Dichotomy try count = 33 guess = 1.4142135621514171
Dichotomy try count = 34 guess = 1.4142135622678325
Dichotomy try count = 35 guess = 1.4142135623260401
Dichotomy try count = 36 guess = 1.414213562355144
Dichotomy try count = 37 guess = 1.4142135623696959
Dichotomy try count = 38 guess = 1.4142135623769718
Dichotomy try count = 39 guess = 1.4142135623733338
Dichotomy try count = 40 guess = 1.4142135623715149
Dichotomy try count = 41 guess = 1.4142135623724243
Dichotomy try count = 42 guess = 1.414213562372879
Dichotomy try count = 43 guess = 1.4142135623731065
Dichotomy try count = 44 guess = 1.4142135623729928
Dichotomy try count = 45 guess = 1.4142135623730496
Dichotomy try count = 46 guess = 1.414213562373078
Dichotomy try count = 47 guess = 1.4142135623730923
Dichotomy try count = 48 guess = 1.4142135623730994
Dichotomy try count = 49 guess = 1.4142135623730958
Dichotomy try count = 50 guess = 1.414213562373094
Dichotomy final result = 1.414213562373094
``````

推荐阅读更多精彩内容

• C语言的学习要从基础开始，这里是100个经典的算法－１C语言的学习要从基础开始，这里是100个经典的 算法 题目：...
Poison_19ce阅读 242评论 0 0
• 转载自http://wanwu.tech/2017/03/15/functions-and-closures/ 简...
quitus阅读 44评论 0 0
• 因为吹水的能力不佳，所以要先打个草稿，今天的吹水过程大概是：1、牛顿迭代法的演绎过程2、牛顿迭代法求n次方根3、牛...
pointertan阅读 1,003评论 0 1
• 牛顿迭代法的作用是使用迭代法来求解函数方程的根，简单的说就是不断地求取切线的过程．对于形如f(x)=0的方程，首先...
Joe_HUST阅读 355评论 0 1
• 转自Poll 的笔记 阅读目录 梯度下降法（Gradient Descent） 牛顿法和拟牛顿法（Newton's...
JSong1122阅读 439评论 0 2