www.gusucode.com > simulink 案例源码程序 matlab代码 > simulink/SweepParameterValueAndInspectResultsExample.m
%% Sweep Parameter Value and Inspect Simulation Results % This example shows how to change a block parameter value between multiple % programmatic % simulation runs. Use this technique to determine an optimal parameter % value by comparing the output signal data of each run. % % The example model |sldemo_absbrake| uses a Constant block to specify a % slip setpoint for an anti-lock braking system. Simulate the model with % two different slip setpoint values, 0.24 and 0.25, and compare the output wheel speed of each % simulation run. % % To store the setpoint value, create a % |Simulink.Parameter| object in the base workspace. This technique enables % you to change the value with fewer lines of more readable code. % % Open the example model. open_system('sldemo_absbrake'); %% % The signals |yout| (which is a virtual bus) and |slp| are configured for logging. When you % simulate the model, you can collect and later inspect the values of % these signals by using the Simulation Data Inspector. % % In the block dialog box of the Constant block labeled |Desired relative % slip|, set *Constant value* to |relSlip| and click *Apply*. % % Right-click |relSlip| and select *Create Variable*. % % In the Create New Data dialog box, set *Value* to % |Simulink.Parameter(0.2)| and click *Create*. A |Simulink.Parameter| % object, which stores the parameter value 0.2, appears in the base workspace. % % You can use these commands at the command prompt to create the object and configure the block: relSlip = Simulink.Parameter(0.2); set_param('sldemo_absbrake/Desired relative slip','Value','relSlip') %% % In the |relSlip| property dialog box, click *OK*. % % At the command prompt, configure the model to stream the logged signals to the Simulation Data Inspector. Simulink.sdi.changeLoggedToStreamed('sldemo_absbrake'); %% % Create an array to store the two experimentation values for the relative % slip setpoint, |0.24| and |0.25|. relSlip_vals = [0.24 0.25]; %% % Loop through the experimentation values by setting the value of |relSlip| % to each of the values stored in |relSlip_vals|. Use the |sim| function to % simulate the model after setting the value of |relSlip|. for i = 1:length(relSlip_vals) relSlip.Value = relSlip_vals(i); sim('sldemo_absbrake') end %% % The model streams the logged signals, |yout| and % |slp|, to the Simulation Data Inspector. You can view the signal data % in the Simulation Data Inspector. % % Compare the output data of the two latest simulation runs. runIDs = Simulink.sdi.getAllRunIDs(); runResult = Simulink.sdi.compareRuns(runIDs(end-1), runIDs(end)); %% % Plot the difference between the values of the |Ww| signal (which is an % element of the virtual bus signal |yout|) by specifying the result index |1|. signalResult = getResultByIndex(runResult,1); plot(signalResult.Diff);