title: Broyden's method implementation in Python
body: <p>I am trying to write an implementation of the Broyden method in Python. I have an example in Matlab, trying to rewrite it but unsuccessfully.
  +A is the Jacobian matrix
  F - 4 eloquent matrix:</p>
  
  <pre class="lang-matlab prettyprint-override"><code>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 &lt; 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
  </code></pre>
  
  <p>I am trying to do the same in Python:</p>
  
  <pre class="lang-py prettyprint-override"><code>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;
  </code></pre>
  
  <p>But the result after 3-4 for cycle aren't the same and final result is totally different</p>