~isequal and ~= not working

8 views (last 30 days)
Danielle sal
Danielle sal on 16 Apr 2021
Commented: Walter Roberson on 17 Apr 2021
so I have to vectors that are clearly different but Ive tried runnig the code with both ~isequal and ~= and still doesnt work
function [L,P,D]=eigen(A)
format
[~,n]=size(A);
P=[];
D=[];
L=eig(A);
L=transpose(L);
L=real(L);
L=sort(L);
for i= 1:n-1
Temp1 = L(i);
Temp2 = L(i+1);
if closetozeroroundoff(Temp1-Temp2, 7)== 0
L(1,i+1) = L(1, i);
end
end
if rank(L) ~= n
L = closetozeroroundoff(L,7);
end
fprintf('all eigenvalues of A are\n')
display(L)
%Part II
M = unique(L);
display(M)
m = groupcounts(transpose(L));
M = transpose(M);
for i = 1:size(M)
fprintf('eigenvalue %d has multiplicity %i\n',M(i),m(i))
end
M = unique(L);
[a,b] = size(M);
ValueOfd = zeros(1:b);
P = zeros(n,1);
for i = 1:b
space = A - (eye(n)*M(i));
W = null(space);
P = horzcat(P,W);
fprintf('a basis for the eigenspace for lambda=%d is:\n',M(i))
display(W)
d = size(W,2);
ValueOfd(i) = d;
fprintf('dimension of eigenspace for lambda = %d is %i\n',M(i),d)
end
P(:,1) = [];
%Part 3
disp(m)
disp(transpose(ValueOfd))
if ~eq(transpose(ValueOfd), m)
disp("A is not diagonalizable")
else
disp("A is diagonalizable")
This is the function ad then I run it for
%(b)
A=[2 4 3;-4 -6 -3;3 3 1]
[L,P,D]=eigen(A);
any idea on how to fix this?

Answers (1)

Walter Roberson
Walter Roberson on 17 Apr 2021
Edited: Walter Roberson on 17 Apr 2021
if ~isequal(transpose(ValueOfd), m)
disp("A is not diagonalizable")
else
disp("A is diagonalizable")
end
  1 Comment
Walter Roberson
Walter Roberson on 17 Apr 2021
Remember isequal will not forgive floating-point round off differences.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!