python牛顿迭代法_python-来自维基百科示例的Gauss-Newton方法的实现
我是Python的新手,正在嘗試實現Gauss-Newton方法,特別是在Wikipedia頁面上的示例(Gauss–Newton algorithm,第3個示例).以下是我到目前為止所做的:
import scipy
import numpy as np
import math
import scipy.misc
from matplotlib import pyplot as plt, cm, colors
S = [0.038,0.194,.425,.626,1.253,2.500,3.740]
rate = [0.050,0.127,0.094,0.2122,0.2729,0.2665,0.3317]
iterations = 5
rows = 7
cols = 2
B = np.matrix([[.9],[.2]]) # original guess for B
Jf = np.zeros((rows,cols)) # Jacobian matrix from r
r = np.zeros((rows,1)) #r equations
def model(Vmax, Km, Sval):
return ((vmax * Sval) / (Km + Sval))
def partialDerB1(B2,xi):
return round(-(xi/(B2+xi)),10)
def partialDerB2(B1,B2,xi):
return round(((B1*xi)/((B2+xi)*(B2+xi))),10)
def residual(x,y,B1,B2):
return (y - ((B1*x)/(B2+x)))
for i in range(0,iterations):
sumOfResid=0
#calculate Jr and r for this iteration.
for j in range(0,rows):
r[j,0] = residual(S[j],rate[j],B[0],B[1])
sumOfResid = sumOfResid + (r[j,0] * r[j,0])
Jf[j,0] = partialDerB1(B[1],S[j])
Jf[j,1] = partialDerB2(B[0],B[1],S[j])
Jft = np.transpose(Jf)
B = B + np.dot((np.dot(Jft,Jf)**-1),(np.dot(Jft,r)))
print B
殘差的平方和在每次迭代時都增加而不是趨向于0,并且我得到的B向量增加.
我在了解問題所在時遇到了麻煩,我們將不勝感激.
解決方法:
您在Beta更新的代碼中出了錯:應該是
B = B - np.dot(np.dot( inv(np.dot(Jft, Jf)), Jft), r)
而不是矩陣上的**-1來計算逆矩陣
import scipy
import numpy as np
from numpy.linalg import inv
import math
import scipy.misc
#from matplotlib import pyplot as plt, cm, colors
S = [0.038,0.194,.425,.626,1.253,2.500,3.740]
rate = [0.050,0.127,0.094,0.2122,0.2729,0.2665,0.3317]
iterations = 5
rows = 7
cols = 2
B = np.matrix([[.9],[.2]]) # original guess for B
print(B)
Jf = np.zeros((rows,cols)) # Jacobian matrix from r
r = np.zeros((rows,1)) #r equations
def model(Vmax, Km, Sval):
return ((Vmax * Sval) / (Km + Sval))
def partialDerB1(B2,xi):
return round(-(xi/(B2+xi)),10)
def partialDerB2(B1,B2,xi):
return round(((B1*xi)/((B2+xi)*(B2+xi))),10)
def residual(x,y,B1,B2):
return (y - ((B1*x)/(B2+x)))
#
for _ in xrange(iterations):
sumOfResid=0
#calculate Jr and r for this iteration.
for j in xrange(rows):
r[j,0] = residual(S[j],rate[j],B[0],B[1])
sumOfResid += (r[j,0] * r[j,0])
Jf[j,0] = partialDerB1(B[1],S[j])
Jf[j,1] = partialDerB2(B[0],B[1],S[j])
Jft = Jf.T
B -= np.dot(np.dot( inv(np.dot(Jft,Jf)),Jft),r)
print B
標簽:scipy,python,numpy
來源: https://codeday.me/bug/20191120/2044228.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的python牛顿迭代法_python-来自维基百科示例的Gauss-Newton方法的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python有多少关键字_Python挖
- 下一篇: proteus仿真micropython