Main Content

More Efficient Batch Linearization Varying Parameters

This example shows how to speed up the batch linearization of a model when a set of model parameters are varied.

To decrease the linearization time for such a model, you can pass the varying parameter values to thelinearizefunction.linearizeavoids 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.

Plant Model

In this example, you linearize a lightweight airplane model. For more information on this model, seeLightweight Airplane Design(Aerospace Blockset). Using this model requires Aerospace Blockset™ software.

Open the model.

mdl ='scdskyhogg'; open_system(mdl)

Extract the linearization inputs and outputs from the model.

io = getlinio(mdl);

Extract the initial operating point of the model.

op = operpoint(mdl);

Linearize Model By CallinglinearizeMultiple Times

For this example, you vary the gains of the altitude and pitch controllers in the autopilot system by +/- 10%.

Open the auto pilot subsystem.

open_system('scdskyhogg/Vehicle System Model/Avionics/Autopilot')

Initialize the gains of the controllers to vary with MATLAB® workspace variablesk1andk2.

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

指定不同的值k1andk2.

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

Linearize the model for the specified values ofk1andk2by calling thelinearizefunction 20 times.

t = cputime;fori=1:20 k1 = k1val(i); k2 = k2val(i); sys_forloop(:,:,i) = linearize(mdl,op,io);end

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

dt_for = cputime - t
dt_for = 87.9000

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 Model By Passing Parameter Values tolinearize

Instead of callinglinearizeseparately for each combination of parameter values, you can pass the parameter values tolinearizein a single function call.

Define the parameter values in a structure by specifying the name of the MATLAB workspace variables and the value arrays.

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

Linearize the model.

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

View the total time to compute the 20 linearizations with one call to thelinearizefunction. In this case, the software compiles the model once when varying the specified parameters.

dt_params = cputime - t
dt_params = 18.3800

Compare Results

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',“使用参数线性化结构”)

Calculate the resulting time improvement ratio.

ratio = dt_for/dt_params
ratio = 4.7824

Close the model.

bdclose(mdl)

See Also

||

Related Topics