Advertisement
legrahamcracker

Broyden's Method for Mat 4010

Dec 11th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. function [ AnsVec, TracF ] = BroydenMethod ( A, x )
  2. # Matthew Graham
  3. # Mat 4010
  4. # Broyden's Method
  5. # A is the matrix in which we are trying to find the eigenvalues and eigenvectors
  6. # x is the initial guess for the eigenvector with the eigenvalue as the last entry
  7. # work in progress as far as comments are concerned
  8. # rest of the code is good to go
  9.  
  10. if (size(A, 1) == size(A, 2))
  11. n = size(A,1);
  12. v = x(1:n);
  13. lambda = x(n+1);
  14.  
  15. for i = 1: n
  16.  
  17. F(i) = [A(i,:)*v];
  18. i = i+1;
  19.  
  20. endfor
  21.  
  22. F = F';
  23. F = [F; .5*(v'*v)] + [-lambda*v; -1];
  24.  
  25. TracF = [F];
  26.  
  27. J = [A-lambda*eye(n), -v; v', 0];
  28. d = -J\F;
  29.  
  30. x = [x, x(:, columns(x))+d];
  31. delta = x(:, columns(x)-1) - x(:, columns(x));
  32.  
  33. v = x(1:n, columns(x));
  34. lambda = x(n+1, columns(x));
  35.  
  36. for i = 1: n
  37.  
  38. F(i) = [A(i,:)*v];
  39. i = i+1;
  40.  
  41. endfor
  42.  
  43. F(n+1) = .5*(v'*v);
  44. F = F + [-lambda*v; -1];
  45.  
  46. TracF = [TracF, F];
  47. y = TracF(:, columns(TracF)-1) - TracF(:, columns (TracF));
  48.  
  49. J = J + ((y - J*delta)/(delta'*delta))*delta';
  50.  
  51. while abs((sqrt(delta'*delta))/sqrt((x(:, columns(x))'*x(:, columns(x)))))>1e-6
  52.  
  53. d = -J\F;
  54.  
  55. x = [x, x(:, columns(x))+d];
  56. delta = x(:, columns(x)-1) - x(:, columns(x));
  57.  
  58. v = x(1:n, columns(x));
  59. lambda = x(n+1, columns(x));
  60.  
  61. for i = 1: n
  62.  
  63. F(i) = [A(i,:)*v];
  64. i = i+1;
  65.  
  66. endfor
  67.  
  68. F(n+1) = .5*(v'*v);
  69. F = F + [-lambda*v; -1];
  70.  
  71. TracF = [TracF, F];
  72. y = TracF(:, columns(TracF)-1) - TracF(:, columns (TracF));
  73.  
  74. J = J + ((y - J*delta)/(delta'*delta))*delta';
  75.  
  76. endwhile
  77.  
  78. AnsVec = x(:, columns(x));
  79.  
  80. else
  81. printf("Matrix A is not square \n")
  82. endif
  83.  
  84. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement