Givens rotation method to find eigen values
18 次查看(过去 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!