MATLAB: How to execute a subsystem and its input at two different rates during a simulation in Simulink 7.9 (R2012a)

simulink

I have a subsystem that I would like to execute at different rates based on some event or state in the model. The event could be the time of the simulation. Initially the subsystem should import data and execute at a particular rate. After 10 seconds, the subsystem should import data and execute at a different rate.

Best Answer

This can be performed using a MATLAB Function block with a Triggered Subsystem.
The MATLAB Function block can be configured to take a clock input and output an event at a particular rate based on the time.
The attached demo model "testconditional" performs this functionality. The following example indicates what the MATLAB Function block code would look like to execute a subsystem called "controller" on a Sine Wave which is sampled at 0.1 seconds. The simulation Sample Time is 0.1 seconds. Before 10 seconds, the subsystem controller is executed at every simulation step or every 0.1 seconds. After 10 seconds, the subsystem controller is executed every 1 second.
function fcn(u)
%#codegen
persistent ii
if isempty(ii)
ii = 0;
end
ii = ii + 1;
if u < 10
% if time u is less than 10 seconds, execute controller every 0.1 seconds
myfcncall();
ii = 0;
else
% if time u is greater than 10 seconds, execute controller every 0.1*10 = 1
% seconds
if ii > 10
myfcncall();
ii = 0;
end
end
Related Question