%% Sparse Second-Order Model in a Feedback Loop
% For this example, consider sparseSOSignal.mat that contains a sparse second-order model. Define an actuator, sensor, and controller and connect them together with the plant in a feedback loop.
% Load the sparse matrices and create the mechss object.

load sparseSOSignal.mat
plant = mechss(M,C,K,B,F,[],[],'Name','Plant');

% Next, create an actuator and sensor using transfer functions.

act = tf(1,[1 0.5 3],'Name','Actuator');
sen = tf(1,[0.02 7],'Name','Sensor');

% Create a PID controller object for the plant.

con = pid(1,1,0.1,0.01,'Name','Controller');

% Use the feedback command to connect the plant, sensor, actuator, and controller in a feedback loop.

sys = feedback(sen*plant*act*con,1)

% The resultant system sys is a mechss object since mechss objects take precedence over all other model object types.
% Use showStateInfo to view the component and signal groups.

showStateInfo(sys)

% Use xsort to sort the components and signals, and then view the component and signal groups.

sysSort = xsort(sys);
showStateInfo(sysSort)

% Observe that the components are now ordered before the signal partition. The signals are now sorted and grouped together in a single partition.
% You can also visualize the sparsity pattern of the resultant system using spy.

spy(sysSort)

% Copyright 2020 The MathWorks, Inc.