This example shows how to speed up the batch linearization of a model when a set of model parameters are varied.
The decrease the linearization time, you pass the varying parameter values to the linearize
function. linearize
avoids recompiling the model when the varied parameters 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™ software.
In this example, you linearize a lightweight airplane model. For more information on this model, see Lightweight Airplane Design (Aerospace Blockset).
Open the model.
mdl = 'scdskyhogg';
open_system(mdl)
io = getlinio(mdl);
op = operpoint(mdl);
linearize
Multiple TimesFor this example, you vary the gains of the altitude and pitch controllers by +/- 10%.
Initialize the gains of the controllers to vary with MATLAB® workspace variables 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')
Vary the values of k1
and k2
and linearize the model 20
times.
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 = 50.0100
A factor that impacts this time is the total time it takes to compile and evaluate block masks and resolve workspace parameters. To identify bottlenecks in your model compilation, use the MATLAB Profiler.
linearize
To pass the parameter values to linearize
, specify the parameter names and values 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 the linearize
command. In this case, the model is compiled only once when varying the specified parameters.
dt_params = cputime - t
dt_params = 10.1900
In this example, the varying parameters do not impact the operating point of the Simulink model. The linearizations using both approaches are equivalent.
bode(sys_forloop(:,:,1),sys_params(:,:,1)) legend('Linearization in FOR loop','Linearization using parameter structure')
Calculate the resulting time improvement ratio.
ratio = dt_for/dt_params
ratio = 4.9078
Close the model.
bdclose(mdl)