牛顿迭代法解超越方程
  
      
       
        
        
          L 
         
        
          = 
         
         
          
          
            g 
           
           
           
             T 
            
           
             2 
            
           
          
          
          
            2 
           
          
            π 
           
          
         
        
          t 
         
        
          a 
         
        
          n 
         
        
          h 
         
        
          ( 
         
         
          
          
            2 
           
          
            π 
           
          
         
           L 
          
         
        
          d 
         
        
          ) 
         
        
       
         L=\frac{gT^2}{2\pi}tanh(\frac{2\pi}{L}d) 
        
       
     L=2πgT2tanh(L2πd)
方程:
f ( L ) = L − g T 2 2 π t a n h ( 2 π L d ) = 0 f(L)=L-\frac{gT^2}{2\pi}tanh(\frac{2\pi}{L}d)=0 f(L)=L−2πgT2tanh(L2πd)=0
导数为:
  
      
       
        
         
         
           f 
          
          
           
          
            ′ 
           
          
         
        
          ( 
         
        
          L 
         
        
          ) 
         
        
          = 
         
        
          1 
         
        
          − 
         
         
          
          
            g 
           
           
           
             T 
            
           
             2 
            
           
          
            d 
           
          
          
           
           
             L 
            
           
             2 
            
           
          
            c 
           
          
            o 
           
          
            s 
           
           
           
             h 
            
           
             2 
            
           
          
            ( 
           
           
            
            
              2 
             
            
              π 
             
            
           
             L 
            
           
          
            d 
           
          
            ) 
           
          
         
        
       
         f^{'}(L)=1-\frac{gT^2d}{L^2cosh^2(\frac{2\pi}{L}d)} 
        
       
     f′(L)=1−L2cosh2(L2πd)gT2d
原理:
 
 代码:
# 函数表达式x^3-2x+1
def fun(L):
    return L-9.8*10**2/(2*np.pi)*np.tanh(2*np.pi/L*40)
 
# 函数一阶导3x^2-2
def fun2(L):
    return 1+9.8*10**2*40*(1/np.cosh(2*np.pi*40/L))**2/(L**2)
# 迭代,参数:初始值、最大迭代次数、最小误差
def get_root(x0, max_iter=30, tol=1e-2):
    # 初始值浮点化
    p0 = x0 * 1.0
    # 最大50次迭代循环
    for i in range(max_iter):
        # 函数的一阶导不能为0,最普遍的说法是不能非正定
        p = p0 - fun(p0) / fun2(p0)
        # print(p)
        # 如果小于误差精度值则退出迭代
        if abs(p - p0) < tol:
            return "经过{}次迭代,我们估计的参数值是{}".format(i + 1, p)
        # 赋值重新迭代
        p0 = p
    print("无法迭代")
if __name__ == '__main__':
    print(get_root(200))
#     print(get_root(0))
#     print(get_root(-2))
 
输出:
经过3次迭代,我们估计的参数值是146.25131148928313


![[ 云计算 | Azure ] Chapter 06 | 计算服务之虚拟机、虚拟机规模集、Azure 容器、Azure App 与 Azure Functions](https://img-blog.csdnimg.cn/6c9f18d7b640421985dfe70215dad0dc.png)







![C嘎嘎~~ [类 下篇(2)]](https://img-blog.csdnimg.cn/7a2a80049044454e9edb8de6c6241cea.png)








