程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

牛頓下山法(Python實現)

編輯:Python


目錄

 ​1、原理​​

 ​2、案例 ​​

 ​3、Python實現 ​​

 ​4、結果​​

 ​5、展望​​


1、原理

2、案例 

3、Python實現 

import numpy as np


# Store the iteration value of each step
result = []
# Store downhill factors for each step
all_r = []


# Judge whether it is singular. True is singular and false is nonsingular
def strange(xk):
return True if (0.5*xk**(-1/2)-3*xk**2) == 0 else False


# Primitive function
def fx(xk):
return xk**(1/2)-xk**3+2


# Newton iterative formula
# xk is the iterative value and r is the downhill factor
def nd(xk, r):
return xk - fx(xk) / (0.5*xk**(-1/2)-3*xk**2) * r


# Newton downhill formula
# return True Downhill success False Downhill failure
def nd_xs(xk, m):
r = 1
count = 1
while True:
if count > m:
return False

xk1 = nd(xk, r)
if abs(fx(xk1)) < abs(fx(xk)):
result.append(xk1)
all_r.append(r)
return True
else:
r *= 0.5

count += 1


# Main function
def main():
# initial value
x = float(input("Please enter the initial value:"))
result.append(x)
# Error limit
e = float(input("Please enter the error limit:"))
# Maximum number of iterations
n = int(input("Please enter the maximum number of iterations:"))
# Maximum number of downhill
m = int(input("Please enter the maximum number of times to go down the mountain:"))

# Number of iterations
ite = 1
while True:
if ite > n:
print("Number of iterations exceeded!")
return

if strange(result[-1]):
print("Singular, denominator zero!")
return

if not nd_xs(result[-1], m):
print("Downhill failure!")
return

if abs(result[-1] - result[-2]) < e:
print("Downhill factor of each step:" + str(all_r))
print("Iteration value of each step (including initial value):" + str(result))
return

ite += 1

if __name__ =='__main__':
main()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.

4、結果

Please enter the initial value:1.5

Please enter the error limit:0.00001
Please enter the maximum number of iterations:100000
Please enter the maximum number of times to go down the mountain:100000
Downhill factor of each step:[1, 1, 1]
Iteration value of each step (including initial value):[1.5, 1.4763069991556952, 1.4758905899820982, 1.4758904626019806]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

5、展望

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved