关于MIT 6.00课程lec06--牛顿法

发布于 2024-12-20 08:15:27 字数 843 浏览 1 评论 0原文

我尝试用自己的方式编码,但发现我得到了错误的答案。

我已阅读此页面。并尝试启动该过程:

在此处输入图像描述

f(x)=x^2-e

数学:

在此处输入图像描述

所以这是我的代码:

def sqrtRootNR(num, count, epsl):
    """
    for test
    """
    num = float(num)
    guess = num / 2.0
    diff = guess ** 2.0 - num
    _cnt = 0
    while abs(diff) > epsl and _cnt < count:
        guess = guess - (guess ** 2.0 + epsl) / (guess * 2.0)
        diff = guess ** 2.0 - num
        _cnt = _cnt +1
    print guess, _cnt

sqrtRootNR(2, 100, 0.0001)

但是,我得到了错误的答案。

该函数的输出是:

D:\poc>python sq.py

0.0595177826557 100

I have tried to code in my own way, but found I got the wrong answer.

I have read this page. And try to start the process:

enter image description here

f(x)=x^2-e

The math:

enter image description here

So there is my code:

def sqrtRootNR(num, count, epsl):
    """
    for test
    """
    num = float(num)
    guess = num / 2.0
    diff = guess ** 2.0 - num
    _cnt = 0
    while abs(diff) > epsl and _cnt < count:
        guess = guess - (guess ** 2.0 + epsl) / (guess * 2.0)
        diff = guess ** 2.0 - num
        _cnt = _cnt +1
    print guess, _cnt

sqrtRootNR(2, 100, 0.0001)

However, I got the wrong answer.

The output of this function is:

D:\poc>python sq.py

0.0595177826557 100

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶花眉 2024-12-27 08:15:27

编程的一项重要技能是知道哪些信息最有用。如果添加一些简单的调试信息:

while abs(diff) > epsl and _cnt < count:
    guess = guess - (guess ** 2.0 + epsl) / (guess * 2.0)
    diff = guess ** 2.0 - num
    print guess, _cnt
    _cnt = _cnt +1
print guess, _cnt

您可以看到您的程序很快就会出错:

$ ./sqrt.py 
0.49995 0
0.249874989999 1
0.124737394941 2
0.0619678553654 3
0.0301770577385 4
0.0134316410297 5
0.00299326718803 6
-0.0152075217183 7
-0.00431591416548 8
0.00942707405618 9
-0.000590335594744 10
....

每次迭代似乎都会将数字减半,直到变为负数,此时行为很难一眼看出。但你可以明显看出,最初的几次迭代是错误的。

对我来说看起来很可疑的东西:(guess ** 2.0 + epsl)

在评估牛顿平方根方法时,您实际上不应该使用 epsilon - 毕竟,您试图确保您的误差小于epsilon。

One important skill in programming is knowing which information where will be most useful. If you add some simple debugging information:

while abs(diff) > epsl and _cnt < count:
    guess = guess - (guess ** 2.0 + epsl) / (guess * 2.0)
    diff = guess ** 2.0 - num
    print guess, _cnt
    _cnt = _cnt +1
print guess, _cnt

You can see that your program goes wrong quickly:

$ ./sqrt.py 
0.49995 0
0.249874989999 1
0.124737394941 2
0.0619678553654 3
0.0301770577385 4
0.0134316410297 5
0.00299326718803 6
-0.0152075217183 7
-0.00431591416548 8
0.00942707405618 9
-0.000590335594744 10
....

It appears to halve the number every iteration until it goes negative, when the behavior gets very difficult to tell just at a glance. But you can obviously tell that the very first few iterations are wrong.

Something that looks quite fishy to me: (guess ** 2.0 + epsl)

You shouldn't actually use epsilon when evaluating Newton's method for square roots -- after all, you're trying to make sure your error is less than epsilon.

你穿错了嫁妆 2024-12-27 08:15:27

看起来您正在寻找函数 f = x^2+eps1 的零点。如果 eps1 为正,则不会有实数零。这意味着您的程序将在某个点之后永远围绕 0 振荡,如您所见。如果将 eps1 设置为负值,我希望您会找到根。

牛顿方法并非万无一失,在某些情况下它可能会发散。

It looks like you are looking for zeroes of the function f = x^2+eps1. If eps1 is positive, there will be no real zeroes. This means that your program will oscillate around 0 forever after a certain point, as you saw. If you set eps1 to a negative value, I expect you would find a root.

Newton's method isn't bullet-proof, and there are cases where it can diverge.

爱的故事 2024-12-27 08:15:27

将方程中的 (guess ** 2.0 + epsl) 更改为 (guess ** 2 - num)。您希望每一步调整您的估计,调整量与您的错误成正比,即。你的 diff 变量。

Change (guess ** 2.0 + epsl) to (guess ** 2 - num) in your equation. You want to adjust your estimate every step by an amount proportional to your error, ie. your diff variable.

述情 2024-12-27 08:15:27

您还可以使用guess = 0.5 * (guess + num/guess)

You also can use guess = 0.5 * (guess + num/guess)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文