Python 使用for循环和if语句的根查找迭代

Python 使用for循环和if语句的根查找迭代,python,Python,我正在使用python进行根查找迭代 我首先定义了F(x) 我还定义了dFdx def dFdx(x): return -0.4+2*x/((1+x**2)**2) 下面是我的根查找代码 def Testing_Root(x, F, dFdx): # store the values of x_n in a list, xstore # first, make an initial list with an initial guess x_0 xstore =

我正在使用python进行根查找迭代

我首先定义了F(x)

我还定义了dFdx

def dFdx(x):
    return -0.4+2*x/((1+x**2)**2)
下面是我的根查找代码

def Testing_Root(x, F, dFdx):
    # store the values of x_n in a list, xstore
    # first, make an initial list with an initial guess x_0
    xstore = [x]
    # calculate differences in each iteration and store in a list, difference
    # first, make an empty list
    difference = []
    print('n=0, x[0] =', x)
    for i in range(50):
        x = x - F(x)/dFdx(x)
        xstore.append(x)
    for j in range(50):
        d = xstore[j+1] - xstore[j]
        difference.append(d)
    for k in range(50):
        print('n=', k+1, 'x[', k+1, ']=', xstore[k+1], 'difference is', difference[k])
        for j in range(k, 50):
            if all(abs(difference[j]) < 0.000000000001):
                break
        break 
    return print('so the root is', round(x, 4), 'to 4dp')

在我的函数中,我想检查它是否收敛到根。另外,我不想打印所有的迭代。我希望它在某个点停止打印迭代,如果该点上所有k>=的差值小于0.000000000001。所以我在for循环中使用了if循环(范围(50)中的'for k):if all(abs(difference[j])三个简单的问题需要解决:

  • all
    设计用于一个iterable(想想:“在列表上”)。通过将
    for
    循环置于
    all
    调用之外,您正在欺骗
    all
    的能力。相反,将感兴趣的整个列表传递给
    all
    并且根本不循环

  • abs
    三件简单的事情需要解决:

    • all
      设计用于一个iterable(想想:“在列表上”)。通过将
      for
      循环置于
      all
      调用之外,您正在欺骗
      all
      的能力。相反,将感兴趣的整个列表传递给
      all
      并且根本不循环
    • abs
      
      
      def Testing_Root(x, F, dFdx):
          # store the values of x_n in a list, xstore
          # first, make an initial list with an initial guess x_0
          xstore = [x]
          # calculate differences in each iteration and store in a list, difference
          # first, make an empty list
          difference = []
          print('n=0, x[0] =', x)
          for i in range(50):
              x = x - F(x)/dFdx(x)
              xstore.append(x)
          for j in range(50):
              d = xstore[j+1] - xstore[j]
              difference.append(d)
          for k in range(50):
              print('n=', k+1, 'x[', k+1, ']=', xstore[k+1], 'difference is', difference[k])
              for j in range(k, 50):
                  if all(abs(difference[j]) < 0.000000000001):
                      break
              break 
          return print('so the root is', round(x, 4), 'to 4dp')
      
      In [38]: Testing_Root(-0.1, F, dFdx)
      n=0, x[0] = -0.1
      n= 1 x[ 1 ]= 0.0004950332214986031 difference is 0.10049503322149861
      so the root is 0.0268 to 4dp
      
      for k in range(50):
          print('n=', k+1, 'x[', k+1, ']=', xstore[k+1], 'difference is', difference[k])
          if all([abs(n)<0.000000000001 for n in difference[k:]]):
              break
      
      n=0, x[0] = -0.1
      n= 1 x[ 1 ]= 0.0004950332214986031 difference is 0.10049503322149861
      n= 2 x[ 2 ]= 0.025061418498996398 difference is 0.024566385277497795
      n= 3 x[ 3 ]= 0.0267848938218123 difference is 0.001723475322815901
      n= 4 x[ 4 ]= 0.02679343238175541 difference is 8.538559943111618e-06
      n= 5 x[ 5 ]= 0.026793432591266996 difference is 2.0951158516413493e-10
      n= 6 x[ 6 ]= 0.026793432591266996 difference is 0.0
      so the root is 0.0268 to 4dp