Tune the control system in the `rct_airframe2` model
to soft goals for tracking, roll off, stability margin, and disturbance
rejection.

Create and configure an `slTuner` interface
to the model.

open_system('rct_airframe2')
st0 = slTuner('rct_airframe2','MIMO Controller');

`st0` is an `slTuner` interface
to the `rct_aircraft2` model with the `MIMO
Controller` block specified as the tunable portion of the
control system.

The model already has linearization input points on the signals `az
ref`, `delta fin`, `az`, `q`,
and `e`. These signals are therefore available as
analysis points for tuning goals and linearization.

Specify the tracking requirement, roll-off requirement,
stability margins, and disturbance rejection requirement.

req1 = TuningGoal.Tracking('az ref','az',1);
req2 = TuningGoal.Gain('delta fin','delta fin',tf(25,[1 0]));
req3 = TuningGoal.Margins('delta fin',7,45);
max_gain = frd([2 200 200],[0.02 2 200]);
req4 = TuningGoal.Gain('delta fin','az',max_gain);

`req1` constrains `az` to
track `az ref`. The next requirement, `req2`,
imposes a roll-off requirement by specifying a gain profile for the
open-loop, point-to-point transfer function measured at `delta
fin`. The next requirement, `req3`, imposes
open-loop gain and phase margins on that same point-to-point transfer
function. Finally, `req4` rejects disturbances to `az` injected
at `delta fin`, by specifying a maximum gain profile
between those two points.

Tune the model using these tuning goals.

opt = systuneOptions('RandomStart',3);
rng(0);
[st,fSoft,~,info] = systune(st0,[req1,req2,req3,req4],opt);

Final: Soft = 1.15, Hard = -Inf, Iterations = 72
Final: Soft = 1.53, Hard = -Inf, Iterations = 86
Final: Soft = 1.15, Hard = -Inf, Iterations = 89
Final: Failed to enforce closed-loop stability (max Re(s) = 0)

`st` is a tuned version of `st0`.

The `RandomStart` option specifies that `systune` must
perform three independent optimization runs that use different (random)
initial values of the tunable parameters. These three runs are in
addition to the default optimization run that uses the current value
of the tunable parameters as the initial value. The call to `rng` seeds
the random number generator to produce a repeatable sequence of numbers.

`systune` displays the final result for each
run. The displayed value, `Soft`, is the maximum
of the values achieved for each of the four performance goals. The
software chooses the best run overall, which is the run yielding the
lowest value of `Soft`. The last run fails to achieve
closed-loop stability, which corresponds to `Soft = Inf`.

Examine the best achieved values of the soft constraints.

fSoft

fSoft =
1.1460 1.1460 0.5434 1.1460

Only `req3`, the stability margin requirement,
is met for all frequencies. The other values are close to, but exceed,
1, indicating violations of the goals for at least some frequencies.

Use `viewSpec` to visualize the tuned control
system performance against the goals and to determine whether the
violations are acceptable. To evaluate specific open-loop or closed-loop
transfer functions for the tuned parameter values, you can use linearization
commands such as `getIOTransfer` and `getLoopTransfer`.
After validating the tuned parameter values, if you want to apply
these values to the Simulink model, you can use `writeBlockValue`.