简体   繁体   English

使用递归的平方根(牛顿算法)

[英]Square Root (Newton's Algorithm) Using Recursion

Can someone explain to me this recursive pseudocode that finds the square root of a number? 有人可以向我解释这个找到数字平方根的递归伪代码吗? I find it difficult to understand since I'm not given what n, p, and e inputs stand for. 我发现很难理解,因为我没有得到n,p和e输入的含义。 Thanks. 谢谢。

if abs(e^2 - n) < p
    SR(n,p,e) = e     
else
    SR(n,p,e) = SR(n,p,(e+n/e)/2)

(e begins at n)

n is the number you want the square root of, e is an estimate for the square root, and p is the precision you want, ie the error you are willing to tolerate. n是您想要的平方根的数字,e是对平方根的估计,p是您想要的精度,即您愿意容忍的误差。 The algorithm says: if e is "close enough" to the answer, ie e^2 is within p of n, then e is the answer you are looking for; 该算法说:如果e与答案“足够接近”,即e ^ 2在n的p之内,则e是您要寻找的答案; otherwise, try a better estimate, (e+n/e)2. 否则,请尝试更好的估计(e + n / e)2。 Why is that a better estimate? 为什么会有更好的估计? if e is larger than sqrt(n), then n/e will be smaller than sqrt(n), so sqrt(n) will be between e and n/e, so try the average of e and n/e as your next estimate. 如果e大于sqrt(n),则n / e小于sqrt(n),因此sqrt(n)将介于e和n / e之间,因此请尝试将e和n / e的平均值作为下一个估计。 (And vice versa if e is less than sqrt(n)). (反之亦然,如果e小于sqrt(n))。

Hope this helps, 希望这可以帮助,

Bruce 布鲁斯

There's rather more to Newton's algorithm than just going from one estimate to a "better estimate". 牛顿算法的功能远不止是从一个估计变为一个“更好的估计”。 There's some detailed maths behind why the better estimate is what it is. 为什么有更好的估计值,后面还有一些详细的数学公式。

The idea is that to find the solution to ANY equation of the form f(x) = 0 , (apart from a few exceptional cases), if you have an approximation to x , you can get a BETTER approximation, by looking at the rate of change of f(x) , which is often written f'(x) , and using that to work out how much you have to adjust your estimate by, to get a much better estimate of the true solution. 这个想法是要找到形式为f(x) = 0 ANY方程的解决方案(除了一些例外情况),如果您对x有一个近似值,则可以通过查看比率来获得更好的近似值f(x)的变化f(x)通常写为f'(x) ,并以此计算出您需要调整的估计量,以获得对真实解的更好估计。

In the case of a square root, that is, we want to find x=sqrt(n) , we can write f(x)=x^2-n and f'(x)=2x , then use Newton's algorithm to find the right x to make f(x)=0 . 在平方根的情况下,即我们要找到x=sqrt(n) ,我们可以写f(x)=x^2-nf'(x)=2x ,然后使用牛顿算法来找到向右x使f(x)=0 What this means is that if we have an estimate e , then to work out our next estimate, we look at f(e)=e^2-n , and we ask how much we have to change e to get rid of this error. 这意味着如果我们有一个估计值e ,然后计算出下一个估计值,我们看f(e)=e^2-n ,我们问我们必须改变多少e才能消除此误差。 。 Since the rate of change of f is f'(x) , which is 2e at the point (e,e^2-n) , we should divide e^2-n by 2e to work out how much we have to adjust e by, to get our next estimate. 由于f的变化率为f'(x) ,在(e,e^2-n)点处为2e ,因此我们应将e^2-n除以2e来算出需要调整e通过,以获得我们的下一个估计。

That is, our next estimate should be 也就是说,我们的下一个估计应该是

  e - (e^2-n) / 2e
= e - (e / 2)  + (n / 2e)
= (e + n / e) / 2

More information on Newton's algorithm can be found at http://tutorial.math.lamar.edu/Classes/CalcI/NewtonsMethod.aspx (which has a lovely diagram that explains how it works) and at http://www.math.brown.edu/UTRA/linapprox.html which goes into some of the more technical detail. 有关牛顿算法的更多信息,请访问http://tutorial.math.lamar.edu/Classes/CalcI/NewtonsMethod.aspx (其中有一张漂亮的图来说明其工作原理)和http://www.math。 brown.edu/UTRA/linapprox.html ,其中包含一些更详细的技术信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM