Accelerating the pace of engineering and science

# Simulink Control Design

## More Efficient Batch Linearization Varying Parameters

This example shows how to use the command LINEARIZE to speed up the batch linearization where a set of block parameters are varied.

Introduction

This example shows how to use the command LINEARIZE to speed up the batch linearization where a set of block parameters are varied. The speed up is achieved by passing the varying parameters to LINEARIZE. LINEARIZE avoids recompiling the model when varying these parameters if they are tunable parameters. The best improvements in the overall linearization time are for models with large model update times. To run this example, you need Aerospace Blockset.

Linearizing the Full Simulink Model 20 Times Calling LINEARIZE in a FOR Loop

In this example, you linearize a model of a lightweight airplane while varying the gain of the altitude and pitch controllers by +/- 10%. Open the Simulink model for lightweight airplane. You can find more information on this model in the example "Lightweight Airplane Design" in Aerospace Blockset examples.

```mdl = 'scdskyhogg';
open_system(mdl);
io = getlinio(mdl);
op = operpoint(mdl);
```

Initialize the gain of the controllers to vary with MATLAB workspace parameters k1 and k2:

```open_system('scdskyhogg/Vehicle System Model/Avionics/Autopilot')
blks = {'scdskyhogg/Vehicle System Model/Avionics/Autopilot/Alt Controller';...
'scdskyhogg/Vehicle System Model/Avionics/Autopilot/Theta Controller'};
set_param(blks{1},'Gain','0.0337283240400683*k1')
set_param(blks{2},'Gain','-261.8699347622*k2')
```

Compute 20 linearizations:

```t = cputime;
for ct = 20:-1:1
k1 = 1+(ct-10)/100;
k2 = 1+(ct-10)/100;
sys_forloop(:,:,ct) = linearize(mdl,op,io);
end
```

View the total time to compute the 20 linearizations in seconds:

```dt_for = cputime - t
```
```dt_for =

39.7900

```

A factor that impacts this time is the total time it takes to compile and evaluate block masks and resolve workspace parameters. The MATLAB Profiler is a recommended tool that you can use to identify bottlenecks in this compilation process.

Linearizing the Simulink Model 20 Times Specifying Parameters to LINEARIZE

We vary only the two parameters. You can use the LINEARIZE command syntax where the parameter names and values are passed using a structure:

```ct = 1:20;
k1val = 1+(ct-10)/100;
k2val = 1+(ct-10)/100;

params(1).Name = 'k1';
params(1).Value = k1val;
params(2).Name = 'k2';
params(2).Value = k2val;

t = cputime;
sys_params = linearize(mdl,op,io,params);
```

View the total time to compute the 20 linearizations with one call to LINEARIZE command. Note that the model is compiled only once when varying the specified parameters.

```dt_params = cputime - t
```
```dt_params =

9.8200

```

Comparing the Results

In this example, the parameters varied do not have any impact on the operating point of the Simulink model. The linearizations using both approaches are equivalent.

```bode(sys_forloop(:,:,1),sys_params(:,:,1))
legend('Linearization in a FOR loop','Linearization using parameter structure')
```

Calculate the resulting time improvement ratio:

```ratio = dt_for/dt_params
```
```ratio =

4.0519

```

Close the Simulink model

```bdclose(mdl);
```