我试图用Python编写Broyden方法的实现。我在Matlab中有一个示例,尝试重写它,但未成功。 + A是雅可比矩阵 F-4雄辩矩阵:
function F=f(X)
F(1)=X(1)+2*X(2)+X(3)+4*X(4)-20.7;
F(2)=X(1)^2+2*X(1)*X(2)+X(4)^3-15.88;
F(3)=X(1)^3+X(3)^2+X(4)-21.218;
F(4)=3*X(2)+X(3)*X(4)-7.9;
F=F(:);
for iii=1:itmax
deltax=-A\f(x);
x=x+deltax; fi1=f(x);
%A=A+(fi1-fi-A*deltax)*deltax'/(deltax'*deltax);
a1 = A*deltax;
a2 = fi1-fi;
a3 = a2-a1;
a4 = a3*deltax';
a5 = deltax'*deltax;
a6 = a4/a5;
A=A+a6;
tikslumas=norm(deltax)/(norm(x)+norm(deltax));
if tikslumas < eps
fprintf(1,'\n x ='); fprintf(1,' %g',x);
fprintf(1,'\n f ='); fprintf(1,' %g',f(x));
break
elseif iii == itmax
fprintf(1,'\n ****failed: x ='); fprintf(1,' %g',x);
fprintf(1,'\n f ='); fprintf(1,' %g',f(x));
break
fi=fi1;
end
我正试图在Python中做同样的事情:
for i in range(1, itmax):
deltax,resid,rank,s = np.linalg.lstsq(A, f(x))
for j in range(len(deltax)):
deltax[j] = -deltax[j]
for j in range(len(deltax)):
x[j] = x[j] + deltax[j]
fi1=f(x);
deltaxT = np.matrix(deltax).T
a1 = np.multiply(A, deltax)
a1 = a1.sum(axis=1)
a2 = np.subtract(fi1,fi)
a3 = np.subtract(a2,a1)
a4 = np.multiply(a3,deltaxT)
a4 = np.matrix(a4).T
a5 = (np.multiply(deltax,deltax)).sum(axis=0) #???? gal???
a6 = np.divide(a4,a5)
A=A+a6;
但是3-4个循环后的结果不一样,最终结果完全不同