%% Approximate Model with Unstable or Near-Unstable Pole
% This example shows how to compute a reduced-order approximation of a system when the system has unstable or near-unstable poles.
% When computing a reduced-order approximation, the balred command (or the Model Reducer app) does not eliminate unstable poles because doing so would fundamentally change the system dynamics. Instead, the software decomposes the model into stable and unstable parts and reduces the stable part of the model.
% If your model has near-unstable poles, you might want to ensure that the reduced-order approximation preserves these dynamics. This example shows how to use the Offset option of balred to preserve poles that are close to the stable-unstable boundary. You can achieve the same result in the Model Reducer app, on the Balanced Truncation tab, under Options, using the Offset field, as shown:
%
% Load a model with unstable and near-unstable poles.

load('reduce.mat','gasf35unst')

%
% gasf35unst is a 25-state SISO model with two unstable poles (Re(s) > 0). Examine the system poles to find the near-unstable poles.

pzplot(gasf35unst)
axis([-0.0015 0.0015 -0.0005 0.0005])

% The pole-zero plot shows several poles (marked by x) that fall in the left half-plane, but relatively close to the imaginary axis. These are the near-unstable poles. Two of these fall within 0.0005 of instability. Three more fall within 0.001 of instability.
%
% Examine a Hankel singular-value plot of the model.

hsvplot(gasf35unst)

%
% The plot shows the two unstable modes, but you cannot easily determine the energy contribution of the near-unstable poles. In your application, you might want to reduce the model without discarding those poles nearest to instability, even if they are of relatively low energy. Use the Offset option of balred to calculate a reduced-order system that preserves the two stable poles that are closest to the imaginary axis. The Offset option sets the boundary between poles that balred can discard, and poles that balred must preserve (treat as unstable).

opts = balredOptions('Offset',0.0005);
gasf_arr = balred(gasf35unst,[10 15],opts);

% Providing balred an array of target approximation orders [10 15] causes balred to return an array of approximated models. The array gasf_arr contains two models, a 10th-order and a 15th-order approximation of gasf35unst. In both approximations, balred does not discard the two unstable poles or the two nearly-unstable poles.
%
% Compare the reduced-order approximations to the original model.

 bodeplot(gasf35unst,gasf_arr,'r--')

% The 15th order approximation is a good frequency-domain match to the original model. However, the 10th-order approximation shows changes in high-frequency dynamics, which might be too large to be acceptable. The 15th-order approximation is likely a better choice.
% Copyright 2012 The MathWorks, Inc.