Clear Filters
Clear Filters

Givens rotation method to find eigen values

29 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 24 Dec 2020
Edited: Star Strider on 24 Dec 2020
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 Comments
Walter Roberson
Walter Roberson on 29 Apr 2023
Does this work for any NxN matrix?
Let's test:
N = randi([5 20])
N = 12
B = rand(N,N);
output = given(B)
Rotation 1 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.5065 -0.8622 0 0 0 0 0 0 0 0 0 0 0.8622 0.5065 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.0234 -0.0000 0.5994 0.0497 0.6658 0.1052 0.5269 0.4755 0.2781 0.0554 0.3948 0.4141 0.9188 -0.2728 1.0223 0.5374 0.4288 1.2287 0.8258 0.9532 1.1049 0.8833 0.7321 -0.1618 0.3814 0.0080 -0.0207 0.1000 -0.4026 -0.2694 -0.4508 0.2310 0.0740 -0.3427 -0.6039 0.0792 0.0977 -0.1053 0.7151 0.8861 0.1674 0.2723 0.8783 0.6218 0.5571 0.5978 0.5724 0.5443 0.3911 -0.6282 0.1178 0.8464 0.4570 0.8996 0.2453 0.9406 0.6395 0.9295 0.4592 0.0815 0.5551 0.2063 0.8025 0.5417 0.1634 0.9047 0.7203 0.4828 0.8297 0.8911 0.1929 0.6188 0.7111 -0.3235 0.7717 0.7148 0.8713 0.8243 0.1675 0.0874 0.4499 0.3359 0.1705 0.0229 0.8564 -0.5057 0.7173 0.9990 0.0761 0.2045 0.0383 0.5836 0.7423 0.3946 0.2033 0.0355 0.5731 0.0463 0.0676 0.6150 0.4808 0.4600 0.6727 0.4358 0.8384 0.0820 0.0880 0.6205 0.6614 -0.0242 0.5127 0.9319 0.3060 0.4697 0.8189 0.3950 0.7179 0.6063 0.1282
Rotation 2 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.8629 0 -0.5054 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0.5054 0 0.8629 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.1860 -0.0000 -0.0000 0.0497 0.6658 0.1052 0.5269 0.4755 0.2781 0.0554 0.3948 0.3973 1.3552 -0.2886 0.6474 0.9116 0.4546 1.1978 1.1564 1.1367 1.2350 1.0643 0.9210 -0.1618 0.3186 0.0080 -0.2106 0.1000 -0.4026 -0.2694 -0.4508 0.2310 0.0740 -0.3427 -0.6039 -0.1409 -0.2772 0.0470 0.2787 0.4930 -0.0723 -0.3860 0.3405 0.0548 -0.0777 0.0694 0.1239 0.5443 0.3970 -0.6282 -0.0960 0.8464 0.4570 0.8996 0.2453 0.9406 0.6395 0.9295 0.4592 0.0815 0.8846 0.2063 0.4119 0.5417 0.1634 0.9047 0.7203 0.4828 0.8297 0.8911 0.1929 0.6188 1.0037 -0.3235 0.3064 0.7148 0.8713 0.8243 0.1675 0.0874 0.4499 0.3359 0.1705 0.0229 1.1015 -0.5057 0.1861 0.9990 0.0761 0.2045 0.0383 0.5836 0.7423 0.3946 0.2033 0.0355 0.5287 0.0463 -0.2314 0.6150 0.4808 0.4600 0.6727 0.4358 0.8384 0.0820 0.0880 0.6205 0.8299 -0.0242 0.1081 0.9319 0.3060 0.4697 0.8189 0.3950 0.7179 0.6063 0.1282
Rotation 3 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9991 0 0 -0.0419 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0.0419 0 0 0.9991 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.1870 -0.0000 -0.0000 -0.0000 0.6658 0.1052 0.5269 0.4755 0.2781 0.0554 0.3948 0.4198 1.4091 -0.3147 0.6428 0.8880 0.4734 1.2344 1.1657 1.1751 1.2607 1.1023 0.9394 -0.1618 0.3226 0.0080 -0.2106 0.0866 -0.4026 -0.2694 -0.4508 0.2310 0.0740 -0.3427 -0.6039 -0.1409 -0.2563 0.0470 0.2787 0.5041 -0.0723 -0.3860 0.3405 0.0548 -0.0777 0.0694 0.1239 0.5272 0.3735 -0.6156 -0.1231 0.7925 0.4376 0.8487 0.1966 0.8921 0.5872 0.8841 0.4202 0.0815 0.9065 0.2063 0.4119 0.5042 0.1634 0.9047 0.7203 0.4828 0.8297 0.8911 0.1929 0.6188 1.0327 -0.3235 0.3064 0.6722 0.8713 0.8243 0.1675 0.0874 0.4499 0.3359 0.1705 0.0229 1.1424 -0.5057 0.1861 0.9520 0.0761 0.2045 0.0383 0.5836 0.7423 0.3946 0.2033 0.0355 0.5540 0.0463 -0.2314 0.5923 0.4808 0.4600 0.6727 0.4358 0.8384 0.0820 0.0880 0.6205 0.8682 -0.0242 0.1081 0.8963 0.3060 0.4697 0.8189 0.3950 0.7179 0.6063 0.1282
Rotation 4 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.8722 0 0 0 -0.4892 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0.4892 0 0 0 0.8722 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.3610 -0.0000 -0.0000 -0.0000 -0.0000 0.1052 0.5269 0.4755 0.2781 0.0554 0.3948 0.4060 1.6997 -0.1735 0.7621 1.0211 -0.3883 1.5192 1.3691 1.2611 1.5054 1.3973 0.9137 -0.1618 0.0844 0.0080 -0.2106 0.0866 -0.5089 -0.2694 -0.4508 0.2310 0.0740 -0.3427 -0.6039 -0.1409 -0.2589 0.0470 0.2787 0.5041 0.0623 -0.3860 0.3405 0.0548 -0.0777 0.0694 0.1239 0.5272 0.5398 -0.6156 -0.1231 0.7925 0.1990 0.8487 0.1966 0.8921 0.5872 0.8841 0.4202 -0.1343 0.0448 0.3339 0.0448 0.0054 -0.1273 0.1852 0.0580 -0.1538 0.1069 0.2380 -0.2912 0.6188 1.3269 -0.3235 0.3064 0.6722 0.2547 0.8243 0.1675 0.0874 0.4499 0.3359 0.1705 0.0229 1.0336 -0.5057 0.1861 0.9520 -0.4924 0.2045 0.0383 0.5836 0.7423 0.3946 0.2033 0.0355 0.7184 0.0463 -0.2314 0.5923 0.1484 0.4600 0.6727 0.4358 0.8384 0.0820 0.0880 0.6205 0.9069 -0.0242 0.1081 0.8963 -0.1578 0.4697 0.8189 0.3950 0.7179 0.6063 0.1282
Rotation 5 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9970 0 0 0 0 -0.0771 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0.0771 0 0 0 0 0.9970 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.3651 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.5269 0.4755 0.2781 0.0554 0.3948 0.4524 1.9132 -0.1979 0.7835 1.0699 -0.3675 1.4350 1.3779 1.2641 1.5356 1.4190 0.9241 -0.1618 0.0634 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4508 0.2310 0.0740 -0.3427 -0.6039 -0.1409 -0.2878 0.0470 0.2787 0.5041 0.0623 -0.3649 0.3405 0.0548 -0.0777 0.0694 0.1239 0.5272 0.6036 -0.6156 -0.1231 0.7925 0.1990 0.8046 0.1966 0.8921 0.5872 0.8841 0.4202 -0.1343 0.0590 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0580 -0.1538 0.1069 0.2380 -0.2912 0.5856 1.2428 -0.3092 0.2468 0.5915 0.2839 0.6108 0.0615 -0.0100 0.3325 0.2272 0.0996 0.0229 1.0463 -0.5057 0.1861 0.9520 -0.4924 0.1242 0.0383 0.5836 0.7423 0.3946 0.2033 0.0355 0.7517 0.0463 -0.2314 0.5923 0.1484 0.4033 0.6727 0.4358 0.8384 0.0820 0.0880 0.6205 0.9404 -0.0242 0.1081 0.8963 -0.1578 0.3984 0.8189 0.3950 0.7179 0.6063 0.1282
Rotation 6 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9329 0 0 0 0 0 -0.3601 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0.3601 0 0 0 0 0 0.9329 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.4632 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.4755 0.2781 0.0554 0.3948 0.4303 2.4844 -0.3667 0.7979 1.3409 -0.5202 1.3835 0.4338 1.3894 1.6999 1.4659 0.9353 -0.1618 -0.1032 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4434 0.2310 0.0740 -0.3427 -0.6039 -0.1409 -0.1459 0.0470 0.2787 0.5041 0.0623 -0.3649 0.4213 0.0548 -0.0777 0.0694 0.1239 0.5272 0.6339 -0.6156 -0.1231 0.7925 0.1990 0.8046 -0.0339 0.8921 0.5872 0.8841 0.4202 -0.1343 0.0759 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0329 -0.1538 0.1069 0.2380 -0.2912 0.5856 1.1816 -0.3092 0.2468 0.5915 0.2839 0.6108 -0.3901 -0.0100 0.3325 0.2272 0.0996 -0.1415 0.1022 -0.4005 -0.1085 0.5029 -0.3271 -0.4008 -0.5330 0.0893 0.1396 -0.1428 -0.1431 0.0355 0.9435 0.0463 -0.2314 0.5923 0.1484 0.4033 0.3569 0.4358 0.8384 0.0820 0.0880 0.6205 1.1722 -0.0242 0.1081 0.8963 -0.1578 0.3984 0.4254 0.3950 0.7179 0.6063 0.1282
Rotation 7 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9511 0 0 0 0 0 0 -0.3090 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0.3090 0 0 0 0 0 0 0.9511 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.5385 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.2781 0.0554 0.3948 0.4203 2.9744 -0.3345 0.6874 1.4583 -0.4488 1.4404 0.5228 0.5645 1.8758 1.4195 0.9167 -0.1618 -0.0268 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4434 0.2516 0.0740 -0.3427 -0.6039 -0.1409 -0.1219 0.0470 0.2787 0.5041 0.0623 -0.3649 0.4213 0.0972 -0.0777 0.0694 0.1239 0.5272 0.8786 -0.6156 -0.1231 0.7925 0.1990 0.8046 -0.0339 0.6526 0.5872 0.8841 0.4202 -0.1343 0.0247 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0329 -0.1697 0.1069 0.2380 -0.2912 0.5856 1.1206 -0.3092 0.2468 0.5915 0.2839 0.6108 -0.3901 -0.3747 0.3325 0.2272 0.0996 -0.1415 0.1248 -0.4005 -0.1085 0.5029 -0.3271 -0.4008 -0.5330 0.0533 0.1396 -0.1428 -0.1431 -0.0992 0.1186 0.1573 -0.4666 0.1490 0.3019 -0.0440 0.2054 -0.0542 0.2720 -0.3750 -0.2054 0.6205 1.2369 -0.0242 0.1081 0.8963 -0.1578 0.3984 0.4254 0.0134 0.7179 0.6063 0.1282
Rotation 8 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9841 0 0 0 0 0 0 0 -0.1779 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0.1779 0 0 0 0 0 0 0 0.9841 0 0
B = 12×12
0.9989 1.5634 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0554 0.3948 0.5239 3.4479 -0.3335 0.6956 1.5945 -0.4697 1.4883 0.5902 0.5579 1.3823 1.5047 0.9249 -0.1618 -0.0132 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4434 0.2516 0.0776 -0.3427 -0.6039 -0.1409 -0.1337 0.0470 0.2787 0.5041 0.0623 -0.3649 0.4213 0.0972 -0.0548 0.0694 0.1239 0.5272 0.9690 -0.6156 -0.1231 0.7925 0.1990 0.8046 -0.0339 0.6526 0.4216 0.8841 0.4202 -0.1343 0.0433 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0329 -0.1697 0.1008 0.2380 -0.2912 0.5856 1.1619 -0.3092 0.2468 0.5915 0.2839 0.6108 -0.3901 -0.3747 0.1279 0.2272 0.0996 -0.1415 0.1476 -0.4005 -0.1085 0.5029 -0.3271 -0.4008 -0.5330 0.0533 0.1152 -0.1428 -0.1431 -0.0992 0.1651 0.1573 -0.4666 0.1490 0.3019 -0.0440 0.2054 -0.0542 0.2466 -0.3750 -0.2054 0.5359 0.7435 0.0357 -0.0159 0.6227 -0.0754 0.1359 0.3256 -0.0872 0.2445 0.3442 -0.0369
Rotation 9 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9994 0 0 0 0 0 0 0 0 -0.0354 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.5644 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 0.3948 0.5312 3.5374 -0.3283 0.6932 1.6186 -0.4606 1.4919 0.5882 0.5650 1.4078 1.4118 0.9391 -0.1618 -0.0253 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4434 0.2516 0.0776 -0.3420 -0.6039 -0.1409 -0.1312 0.0470 0.2787 0.5041 0.0623 -0.3649 0.4213 0.0972 -0.0548 0.0741 0.1239 0.5272 0.9997 -0.6156 -0.1231 0.7925 0.1990 0.8046 -0.0339 0.6526 0.4216 0.8493 0.4202 -0.1343 0.0517 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0329 -0.1697 0.1008 0.2363 -0.2912 0.5856 1.1692 -0.3092 0.2468 0.5915 0.2839 0.6108 -0.3901 -0.3747 0.1279 0.1860 0.0996 -0.1415 0.1425 -0.4005 -0.1085 0.5029 -0.3271 -0.4008 -0.5330 0.0533 0.1152 -0.1479 -0.1431 -0.0992 0.1517 0.1573 -0.4666 0.1490 0.3019 -0.0440 0.2054 -0.0542 0.2466 -0.3806 -0.2054 0.5359 0.7552 0.0357 -0.0159 0.6227 -0.0754 0.1359 0.3256 -0.0872 0.2445 0.3176 -0.0369
Rotation 10 :
S = 12×12
1.0000 0 0 0 0 0 0 0 0 0 0 0 0 0.9696 0 0 0 0 0 0 0 0 0 -0.2447 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0
B = 12×12
0.9989 1.6135 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.5580 4.0167 -0.2381 0.7171 1.6591 -0.4631 1.4939 0.6266 0.5997 1.3658 1.5954 0.1740 -0.1618 -0.1723 0.0080 -0.2106 0.0866 -0.5089 -0.2751 -0.4434 0.2516 0.0776 -0.3420 -0.5794 -0.1409 -0.0969 0.0470 0.2787 0.5041 0.0623 -0.3649 0.4213 0.0972 -0.0548 0.0741 0.1522 0.5272 1.0721 -0.6156 -0.1231 0.7925 0.1990 0.8046 -0.0339 0.6526 0.4216 0.8493 0.1628 -0.1343 -0.0211 0.3339 0.0448 0.0054 -0.1273 0.1812 0.0329 -0.1697 0.1008 0.2363 -0.2950 0.5856 1.1580 -0.3092 0.2468 0.5915 0.2839 0.6108 -0.3901 -0.3747 0.1279 0.1860 -0.1895 -0.1415 0.1031 -0.4005 -0.1085 0.5029 -0.3271 -0.4008 -0.5330 0.0533 0.1152 -0.1479 -0.1736 -0.0992 0.0968 0.1573 -0.4666 0.1490 0.3019 -0.0440 0.2054 -0.0542 0.2466 -0.3806 -0.2363 0.5359 0.7232 0.0357 -0.0159 0.6227 -0.0754 0.1359 0.3256 -0.0872 0.2445 0.3176 -0.2206
Rotation 11 :
Index in position 2 exceeds array bounds. Index must not exceed 12.

Error in solution>given (line 40)
theta = atan(B(1,j+1)/B(1,2));
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

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!