Givens rotation method to find eigen values
조회 수: 16 (최근 30일)
이전 댓글 표시
The below code is to obtain eigen value with the help of Givens rotation method, where the matrix is converted into tridigonal form first and then its eigenvalues are obtained.
The code below works fine for some cases but doesn't give eigenvalues at certain cases.
Ex- A =[-6 2 1;2 -9 -4; 1 -4 -9];
The output is :
root(z^3 + 24*z^2 + 168*z + 361, z, 1)
root(z^3 + 24*z^2 + 168*z + 361, z, 2)
root(z^3 + 24*z^2 + 168*z + 361, z, 3)
Can anyone tell me what I have been missing?
Thankyou for your efforts in advance :)
function eigen_values = given(B)
syms x
[m,n] = size(B);
if nargin>1
fprintf('Please enter only one Matrix');
return;
end
% Eliminating possibilities of error and hence making code more robust
if isscalar(B) || (m ~= n)
fprintf('Please enter a Square matrix \n');
return;
elseif m ==2
fprintf('Please enter a Square matrix grater than 2X2 \n');
return ;
elseif ~isreal(B)
fprintf('All elements of the input matrix should belong to real number domain except infinity and negative infinity \n');
return ;
elseif find((isfinite(B))==0)
fprintf('Please enter a matrix with finite number as its elements \n');
return;
end
S = eye(m); % Preallocating for faster computation
j =2;
N = ((m-1)*(m-2))/2; % Calculating the number of rotaions required to get Tridigonal form
I = eye(m);
theta = atan(B(1,j+1)/B(1,2)); %Calculating theta for first rotation
%The for loop below calculates values of sine and cosine of theta
%It also creates a orthogonal matrix 'S' at each rotation
for i = 1:N
fprintf('Rotation %d : \n',i);
theta = atan(B(1,j+1)/B(1,2));
S(2,2) = cos(theta);
S(2,j+1) = -sin(theta);
S(j+1,2) = sin(theta);
S(j+1,j+1) = cos(theta);
S
B = S'*B*S
j =j+1;
S = eye(m);
if j >m
break;
end
end
eqn = det(B-x*I);
eigen_values = solve(eqn);
end
댓글 수: 0
채택된 답변
Star Strider
2020년 12월 24일
편집: Star Strider
2020년 12월 24일
Instead of solve, use vpasolve here:
eigen_values = vpasolve(eqn);
that with your ‘A’ matrix:
A =[-6 2 1;2 -9 -4; 1 -4 -9];
EV = given(A)
produces:
EV =
-13.596915527892124341934473181412
-5.9128104205174475776683632011464
-4.4902740515904280803971636174419
EDIT — (24 Dec 2020 at 17:12)
To get the result as a vector of double (rather than symbolic) values:
EV = double(EV)
producing:
EV =
-13.596915527892124
-5.912810420517448
-4.490274051590428
.
댓글 수: 4
Walter Roberson
2023년 4월 29일
Does this work for any NxN matrix?
Let's test:
N = randi([5 20])
B = rand(N,N);
output = given(B)
function eigen_values = given(B)
syms x
[m,n] = size(B);
if nargin>1
fprintf('Please enter only one Matrix');
return;
end
% Eliminating possibilities of error and hence making code more robust
if isscalar(B) || (m ~= n)
fprintf('Please enter a Square matrix \n');
return;
elseif m ==2
fprintf('Please enter a Square matrix grater than 2X2 \n');
return ;
elseif ~isreal(B)
fprintf('All elements of the input matrix should belong to real number domain except infinity and negative infinity \n');
return ;
elseif find((isfinite(B))==0)
fprintf('Please enter a matrix with finite number as its elements \n');
return;
end
S = eye(m); % Preallocating for faster computation
j =2;
N = ((m-1)*(m-2))/2; % Calculating the number of rotaions required to get Tridigonal form
I = eye(m);
theta = atan(B(1,j+1)/B(1,2)); %Calculating theta for first rotation
%The for loop below calculates values of sine and cosine of theta
%It also creates a orthogonal matrix 'S' at each rotation
for i = 1:N
fprintf('Rotation %d : \n',i);
theta = atan(B(1,j+1)/B(1,2));
S(2,2) = cos(theta);
S(2,j+1) = -sin(theta);
S(j+1,2) = sin(theta);
S(j+1,j+1) = cos(theta);
S
B = S'*B*S
j =j+1;
S = eye(m);
if j >m
break;
end
end
eqn = det(B-x*I);
eigen_values = vpasolve(eqn);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!