高斯牛顿算法和矩阵乘积
gauss newton algorithm and matricial product
我试图在 python 上做一个高斯-牛顿算法,但我有一个我不明白的错误:
np.dot(-Jr(x0,n,t).T,r(x0,t,a,n))
ValueError: shapes (2,7) and (1,7) not aligned: 7 (dim 1) != 1 (dim 0)`
不确定为什么我的矩阵乘积不起作用...
任何帮助表示赞赏。这是我的代码:
import numpy as np
from scipy import linalg
import math
#on défini ici les residus
def r(x,t,a,n):
r = np.ones((1,n))
r[0,:] = a[0] - x0[0]*np.exp(-x0[1]*t)
return(np.array(r))
#definition du jacobien
def Jr(x,n,t):
Jr = np.ones((n,2))
Jr[:,1] = t*x0[0]*np.exp(-x[1]*t)
Jr[:,0] = -np.exp(-x[1]*t)
return(Jr)
def f(x,n):
z = 0
t = 0
z = (r(x0,t,a,n))**2
for i in range (n):
t = t + z[0][i]
h = math.sqrt(t)
return(h)
#initialisation des variables
t = np.array([500,1000,2000,3000,4000,5000,6300],dtype=float)
a = np.array([14.5,13.5,12.0,10.8,9.9,8.9,8.0],dtype=float).T
x0 = np.array([10,0.0001])
n = np.size(t)
R = (x0,t,a,n)
X = Jr(x0,n,t)
e = 0.00001
k = 0
x = x0
np.dot(-Jr(x0,n,t).T,r(x0,t,a,n))
#while (1/2)*(f(x,n))**2 > e*(1/2)*(f(x0,n))**2:
#s = linalg.solve(np.dot(Jr(x,n,t).T,Jr(x,n,t)),np.dot(- Jr(x,n,t).T,r(x,t,a,n)))
#x = x + s.T
#k = k + 1
#print(x)
#print(k)
谢谢
尝试使用
np.dot(-Jr(x0,n,t).T, r(x0,t,a,n).T)
这应该可以解决维数问题,并将生成形状为 (2,1) 的矩阵。
我试图在 python 上做一个高斯-牛顿算法,但我有一个我不明白的错误:
np.dot(-Jr(x0,n,t).T,r(x0,t,a,n))
ValueError: shapes (2,7) and (1,7) not aligned: 7 (dim 1) != 1 (dim 0)`
不确定为什么我的矩阵乘积不起作用... 任何帮助表示赞赏。这是我的代码:
import numpy as np
from scipy import linalg
import math
#on défini ici les residus
def r(x,t,a,n):
r = np.ones((1,n))
r[0,:] = a[0] - x0[0]*np.exp(-x0[1]*t)
return(np.array(r))
#definition du jacobien
def Jr(x,n,t):
Jr = np.ones((n,2))
Jr[:,1] = t*x0[0]*np.exp(-x[1]*t)
Jr[:,0] = -np.exp(-x[1]*t)
return(Jr)
def f(x,n):
z = 0
t = 0
z = (r(x0,t,a,n))**2
for i in range (n):
t = t + z[0][i]
h = math.sqrt(t)
return(h)
#initialisation des variables
t = np.array([500,1000,2000,3000,4000,5000,6300],dtype=float)
a = np.array([14.5,13.5,12.0,10.8,9.9,8.9,8.0],dtype=float).T
x0 = np.array([10,0.0001])
n = np.size(t)
R = (x0,t,a,n)
X = Jr(x0,n,t)
e = 0.00001
k = 0
x = x0
np.dot(-Jr(x0,n,t).T,r(x0,t,a,n))
#while (1/2)*(f(x,n))**2 > e*(1/2)*(f(x0,n))**2:
#s = linalg.solve(np.dot(Jr(x,n,t).T,Jr(x,n,t)),np.dot(- Jr(x,n,t).T,r(x,t,a,n)))
#x = x + s.T
#k = k + 1
#print(x)
#print(k)
谢谢
尝试使用
np.dot(-Jr(x0,n,t).T, r(x0,t,a,n).T)
这应该可以解决维数问题,并将生成形状为 (2,1) 的矩阵。