How do I minimize this code

1 次查看(过去 30 天)
David Hastana
David Hastana 2020-2-24
回答: Matt J 2020-2-24
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

回答(1 个)

Matt J
Matt J 2020-2-24
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

类别

Help CenterFile Exchange 中查找有关 Stability Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by