# Build Tunable Model for Tuning with `hinfstruct`

This example shows how to construct a tunable model of a control system for tuning with `hinfstruct`. To do so, build a generalized linear model of your closed-loop system, incorporating weighting functions that capture your design requirements (see Formulating Design Requirements as H-Infinity Constraints).

1. Use commands such as `tf`, `zpk`, and `ss` to create numeric linear models that represent the fixed elements of your control system and any weighting functions that represent your design requirements.

2. Use tunable models (either control design blocks or generalized LTI models) to represent the tunable elements of your control system. For more information about tunable models, see Models with Tunable Coefficients.

3. Use model-interconnection commands such as `series`, `parallel`, and `connect` to construct your closed-loop system from the numeric and tunable models.

For this example, build a tunable model of the closed-loop system with weighting functions shown in the following block diagram.

This block diagram represents a head-disk assembly (HDA) in a hard disk drive. The architecture includes the plant `G` in a feedback loop with a PI controller `C` and a low-pass filter, `F = a/(s+a)`. The PI gains of `C` and the filter parameter `a` are tunable to achieve a desired response. For `hinfstruct`, you encode the desired response with the weighting functions `LS` and `1/LS`, which express a target loop shape. Let T(s) denote the closed-loop transfer function from the inputs $\left\{\mathit{r},{\mathit{n}}_{\mathit{w}}\right\}$to the outputs $\left\{\mathit{y},{\mathit{e}}_{\mathit{w}}\right\}$. Then, constraining the ${\mathit{H}}_{\infty }$ norm to less than 1 (${‖\mathit{T}\left(\mathit{s}\right)‖}_{\infty }<1$) approximately enforces the target loop shape.

For this example, use the target loop shape given by:

`$\mathit{LS}=\frac{1+0.001\frac{\mathit{s}}{{\omega }_{\mathit{c}}}}{0.001+\frac{\mathit{s}}{{\omega }_{\mathit{c}}}}$`

This value of `LS` corresponds to the following open-loop response shape.

```wc = 1000; s = tf('s'); LS = (1+0.001*s/wc)/(0.001+s/wc); bodemag(LS,logspace(-2,8,100))```

To prepare for tuning the controller and filter, construct a tunable model of the closed-loop system T(s). First, load the plant model `G`, a ninth-order SISO state-space (`ss`) model.

`load hinfstruct_demo G`

Create a tunable model of the PI controller, using the predefined control design block `tunablePID`.

`C = tunablePID('C','pi');`

There is no predefined control design block for the filter structure `F = a/(s+a)`. You can create the tunable filter using `realp``.`

```a = realp('a',1); F = tf(a,[1 a]);```

To build the closed-loop model, first label all the inputs and outputs of the system components.

```G.InputName = 'u'; G.OutputName = 'y'; We = LS; We.InputName = 'e'; We.OutputName = 'ew'; Wn = 1/LS; Wn.InputName = 'nw'; Wn.OutputName = 'n'; C.InputName = 'e'; C.OutputName = 'u'; F.InputName = 'yn'; F.OutputName = 'yf';```

Specify the summing junctions in terms of the I/O labels of the other components of the control system. One junction takes the difference between the reference signal and the filtered output, producing the error signal `e`. The other junction adds noise to the plant output, producing the noisy output `yn`.

```Sum1 = sumblk('e = r - yf'); Sum2 = sumblk('yn = y + n');```

Finally, use `connect` to combine all the elements into a complete model of the closed-loop system.

`T0 = connect(G,Wn,We,C,F,Sum1,Sum2,{'r','nw'},{'y','ew'});`

`T0` is a `genss` object representing the entire closed-loop control system incorporating the loop-shaping weighting functions. The `Blocks` property of `T0` contains the tunable blocks `C` and `a`. (In this example, the control system model T0 is a continuous-time model, with `T0.Ts = 0`. You can also use `hinfstruct` with a discrete-time model, provided that you specify a definite sample time, `T0.Ts ≠ –1`.)

`T0.Blocks`
```ans = struct with fields: C: [1x1 tunablePID] a: [1x1 realp] ```

You can now use `hinfstruct` to tune `a` and the free parameters of `C`. See Tune and Validate Controller Parameters.