Advertisement
legrahamcracker

Newton's Method for Mat 4010

Dec 11th, 2020 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. function [ AnsVec, TracF ] = NewtonMethod ( A, x )
  2. # Matthew Graham
  3. # Mat 4010
  4. # Newton'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 = length(x)-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. NormF = sqrt(F'*F);
  26.  
  27. TracF = [F];
  28.  
  29. while NormF > 1e-6
  30.  
  31. J = [A-lambda*eye(n), -v; v', 0];
  32. d = J\-F;
  33. x = x + d;
  34.  
  35. v = x(1:n);
  36. lambda = x(n+1);
  37.  
  38. for i = 1: n
  39.  
  40. F(i) = [A(i,:)*v];
  41. i = i+1;
  42.  
  43. endfor
  44.  
  45. F(n+1) = .5*(v'*v);
  46.  
  47. F = F + [-lambda*v; -1];
  48. NormF = sqrt(F'*F);
  49.  
  50. TracF = [TracF, F];
  51.  
  52. endwhile
  53.  
  54. AnsVec = x;
  55.  
  56. else
  57. printf("Matrix A is not square \n")
  58. endif
  59.  
  60. endfunction
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement