My code is not working and i cannot figure out why not?

1 view (last 30 days)
I need to put this into my command window in order to generate my answer d, but my d is not working and i dont know how to fix my d inside my code.
>>rand('seed',1)
>>A = rand(10,10);
>>d = my_det(A)
>>dd = det(A)
>>abs(d-dd)/abs(dd)
function d = my_det(A)
n = size(A,1);
s = 0;
% Gauss_PP:
for k = 1:n-1
r = k;
for j = k+1:n
if abs(A(j,k))> abs(A(k,k))
T=A(k,:);
A(k,:)=A(j,:);
A(j,:)=T;
end
end
while A(r,k) == 0 && r <= n
r=r+1;
end
if r > n+1
disp('A is not invertible');
break
else
if r ~= k
s = s+1;
t=A(k,:);
A(k,:)=A(s,:);
A(s,:)=t;
end
end
d = (-1)^s;;
if A(n,n)< n*10^(-15)
disp('A is not invertible');
return
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 7 Oct 2020
d = (-1)^s;;
Maybe you should be multiplying that by A ?

More Answers (0)

Community Treasure Hunt

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

Start Hunting!