# How do I minimize this code

1 view (last 30 days)
David Hastana on 24 Feb 2020
Answered: Matt J on 24 Feb 2020
How do I minimize this,
A=input('Enter Matrix A: ')
B=input('Enter Matrix B: ')
C=input('Enter Matrix C: ')
n=length(A);
Con=B;
Obs=C;
for x = 1: n-1
Con =[Con, A^(x)*B];
Obs =[Obs, C*A^(x)];
end
[a,b]=size(Con);
[c,d]=size(Obs);
if a==b
if det(Con)~=0
input('The systeme is controlable')
else
input('The systeme is not controlable')
end
elseif a~=b
if rank(Con)==n
input('The systeme is controlable')
else
input('The systeme is not controlable')
end
end
if c==d
if det(Obs)~=0
input('The systeme is observable')
else
input('The systeme is not observable')
end
elseif c~=d
if rank(Obs)==n
input('The systeme is observable')
else
input('The systeme is not observable')
end
end

Matt J on 24 Feb 2020
A=input('Enter Matrix A: ')
B=input('Enter Matrix B: ')
C=input('Enter Matrix C: ')
n=length(A);
Con=cell(1,n); Obs=Con.';
Con{1}=B; Obs{1}=C;
for m = 2: n
Con{m} = A*Con{m-1};
Obs{m} = Obs{m-1}*A;
end
Con=cell2mat(Con); Obs=cell2mat(Obs);
analyzeIt(Con,"controllable",n)
analyzeIt(Obs,"observable",n)
function analyzeIt(Q,msg,n)
[a,b]=size(Q);
threshold=1e-8; %non-singularity tolerance
if (a==b && rcond(Q)>threshold) || (a~=b && rank(Q)==n)
disp("The system is "+msg)
else
disp("The system is not "+msg)
end
end