Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ AnsVec, TracF ] = NewtonMethod ( A, x )
- # Matthew Graham
- # Mat 4010
- # Newton's Method
- # A is the matrix in which we are trying to find the eigenvalues and eigenvectors
- # x is the initial guess for the eigenvector with the eigenvalue as the last entry
- # work in progress as far as comments are concerned
- # rest of the code is good to go
- if (size(A, 1) == size(A, 2))
- n = length(x)-1;
- v = x(1:n);
- lambda = x(n+1);
- for i = 1: n
- F(i) = [A(i,:)*v];
- i = i+1;
- endfor
- F = F';
- F = [F; .5*(v'*v)] + [-lambda*v; -1];
- NormF = sqrt(F'*F);
- TracF = [F];
- while NormF > 1e-6
- J = [A-lambda*eye(n), -v; v', 0];
- d = J\-F;
- x = x + d;
- v = x(1:n);
- lambda = x(n+1);
- for i = 1: n
- F(i) = [A(i,:)*v];
- i = i+1;
- endfor
- F(n+1) = .5*(v'*v);
- F = F + [-lambda*v; -1];
- NormF = sqrt(F'*F);
- TracF = [TracF, F];
- endwhile
- AnsVec = x;
- else
- printf("Matrix A is not square \n")
- endif
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement