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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jacobi迭代法和Gauss-Seidel迭代法均是解线性方程组的迭代方法。下面分别介绍它们的实现方法。 Jacobi迭代法 Jacobi迭代法的公式为: $$x_i^{(k+1)}=\frac{1}{a_{ii}}\left(b_i-\sum_{j=1,j\neq i}^na_{ij}x_j^{(k)}\right),\quad i=1,2,\cdots,n$$ 其中,$a_{ij}$是系数矩阵,$b_i$是常数向量,$x_i^{(k)}$是第$k$次迭代中$x_i$的近似值。 下面是Python实现Jacobi迭代法的代码: ```python import numpy as np def jacobi(A, b, x0, tol=1e-6, max_iter=100): n = len(A) x = x0.copy() for k in range(max_iter): x_new = np.zeros(n) for i in range(n): s = 0 for j in range(n): if j != i: s += A[i, j] * x[j] x_new[i] = (b[i] - s) / A[i, i] if np.linalg.norm(x_new - x) < tol: return x_new x = x_new return x ``` 其中,`A`和`b`分别是系数矩阵和常数向量,`x0`是初始解,`tol`是迭代收敛的容许误差,`max_iter`是最大迭代次数。函数返回迭代得到的近似解。 Gauss-Seidel迭代法 Gauss-Seidel迭代法的公式为: $$x_i^{(k+1)}=\frac{1}{a_{ii}}\left(b_i-\sum_{j=1}^{i-1}a_{ij}x_j^{(k+1)}-\sum_{j=i+1}^na_{ij}x_j^{(k)}\right),\quad i=1,2,\cdots,n$$ 其中,$a_{ij}$是系数矩阵,$b_i$是常数向量,$x_i^{(k)}$是第$k$次迭代中$x_i$的近似值。 下面是Python实现Gauss-Seidel迭代法的代码: ```python import numpy as np def gauss_seidel(A, b, x0, tol=1e-6, max_iter=100): n = len(A) x = x0.copy() for k in range(max_iter): for i in range(n): s1 = sum(A[i, j] * x[j] for j in range(i)) s2 = sum(A[i, j] * x[j] for j in range(i+1, n)) x[i] = (b[i] - s1 - s2) / A[i, i] if np.linalg.norm(A @ x - b) < tol: return x return x ``` 其中,`A`和`b`分别是系数矩阵和常数向量,`x0`是初始解,`tol`是迭代收敛的容许误差,`max_iter`是最大迭代次数。函数返回迭代得到的近似解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值