简体   繁体   English

Python 麦克劳林系列ln(x+1)

[英]Python Maclaurin series ln(x+1)

I have to write a program of Maclaurin series ln(x+1) on Python.我必须在 Python 上编写一个 Maclaurin 级数 ln(x+1) 的程序。

I need to use input function for two values: x, n.我需要将输入 function 用于两个值:x、n。 Then check if the values are legal and calculates the Maclaurin approximation (of order n) of the expression ln (1 + ) around the point x.然后检查这些值是否合法并计算围绕点 x 的表达式 ln (1 + ) 的 Maclaurin 近似值(n 阶)。

*Maclaurin series ln(x+1)= sum of ((-1)^n/n)*x^n *麦克劳林级数 ln(x+1)= ((-1)^n/n)*x^n 之和

I stacked in the end when I calculate to expression, that what I wrote (after all the checks before):当我计算到表达式时,我最后堆叠了我写的内容(在之前的所有检查之后):

for i in range(n + 1):
    if i <= 1:
        continue
    else:
        x = x + (((-1) ** (i + 1)) * (x ** i) / i)

When I input the test I get a number but it's a wrong answer.当我输入测试时,我得到一个数字,但这是一个错误的答案。

Please help me understand what is wrong in this code.请帮助我理解这段代码有什么问题。

You are modifying the value of x in each iteration of the loop.您在循环的每次迭代中修改x的值。 Add and then store the partial sums in another variable.添加并将部分和存储在另一个变量中。

def maclaurin_ln(x, n):
    mac_sum = 0
    for i in range(1, n + 1):
        mac_sum += (((-1) ** (i + 1)) * (x ** i) / i)
    return mac_sum

You can test this with the built-in function log1p to see how close they can get.您可以使用内置的 function log1p进行测试,以查看它们的接近程度。

  1. For ln(2) for different n,对于ln(2)对于不同的 n,
from tabulate import tabulate
res = []
for n in [1, 10, 100, 1000, 10000]:
    p = math.log1p(1)
    q = maclaurin_ln(1, n)
    res.append([1, n, p, q, q-p])
tabulate(res, headers=["x", "n", "log1p", "maclaurin_ln", "maclaurin_ln-log1p"])
  x      n     log1p    maclaurin_ln    maclaurin_ln-log1p
---  -----  --------  --------------  --------------------
  1      1  0.693147        1                  0.306853
  1     10  0.693147        0.645635          -0.0475123
  1    100  0.693147        0.688172          -0.004975
  1   1000  0.693147        0.692647          -0.00049975
  1  10000  0.693147        0.693097          -4.99975e-05
  1. For different x,对于不同的 x,
res = []
for x in range(10):
    p = math.log1p(x/10)
    q = maclaurin_ln(x/10, 100)
    res.append([x/10, 1000, p, q, q-p])
tabulate(res, headers=["x", "n", "log1p", "maclaurin_ln", "maclaurin_ln-log1p"])
  x     n      log1p    maclaurin_ln    maclaurin_ln-log1p
---  ----  ---------  --------------  --------------------
0    1000  0               0                   0
0.1  1000  0.0953102       0.0953102           1.38778e-17
0.2  1000  0.182322        0.182322            2.77556e-17
0.3  1000  0.262364        0.262364           -1.11022e-16
0.4  1000  0.336472        0.336472            0
0.5  1000  0.405465        0.405465           -1.11022e-16
0.6  1000  0.470004        0.470004            5.55112e-17
0.7  1000  0.530628        0.530628           -4.44089e-16
0.8  1000  0.587787        0.587787           -9.00613e-13
0.9  1000  0.641854        0.641854           -1.25155e-07

Mathematically, the Maclaurin series is a bit beyond me, but I'll try to help.从数学上讲,麦克劳林级数有点超出我的想象,但我会尽力提供帮助。 Two things.两件事情。

First, you're storing all the successive values in x, as you calculate them;首先,您在计算时将所有连续值存储在 x 中; that means that the term for n = 5 (i = 5) is using a value of x which isn't the original value of the parameter x, but which has the successive results of the four previous computations stored in it.这意味着 n = 5 (i = 5) 的项使用的 x 的值不是参数 x 的原始值,但它具有存储在其中的四个先前计算的连续结果。 What you need to do instead is something like:您需要做的是:

    total = 0
    for each value:
          this term = some function of x    # the value of x does not change
          total = total + this term

Second, why aren't you interested in the term when i (or n) is equal to 1?其次,为什么你对 i(或 n)等于 1 的术语不感兴趣? The condition条件

   if i <= 1:
       continue

skips out the case when i equals 1, which evaluates to -x.跳过 i 等于 1 的情况,计算结果为 -x。

That should fix it, as far as I can see.据我所知,这应该可以解决它。

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

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