%% Linearize Simulink Model to a Sparse Second-Order Model Object
% This example shows how to linearize a Simulink Model to obtain a sparse second-order model object (mechss). The Simulink model sparseSimulinkModel.slx contains a plant modeled using a Sparse Second Order block connected in a feedback loop with a PID controller and an actuator and sensor. The sensor and actuator are modeled using transfer functions. You need a Simulink Control Design license to perform linearization.
% For more information on sparse models, see Sparse Model Basics.
%
% Load the model data contained in linData.mat and open the Simulink model.

load('linData.mat')
mdl = 'sparseSimulinkModel';
open_system(mdl);

% Next, construct the linearization I/O settings using linio and linearize the Simulink model.

sys_io(1) = linio('sparseSimulinkModel/Controller',1,'input');
sys_io(2) = linio('sparseSimulinkModel/Plant',1,'output');
sys = linearize(mdl,sys_io)

% The resultant linearized model sys is a mechss model object with 3415 degrees of freedom, 1 input and 1 output.
% You can use spy to visualize the sparsity of the sparse model. Right-click on the plot to switch to select the matrices to be displayed.

spy(sys)

% Use showStateInfo to view the partition information of the sparse second-order model sys.

showStateInfo(sys)

% Examine the step response of the sparse second-order model. You must specify the final time or the time vector for sparse models.

t = 0:0.01:20;
step(sys,t)

% Copyright 2020 The MathWorks, Inc.