Programmatic Simulink Running in For Loop

7 views (last 30 days)
Hi all,
I am having a very simple simulink simulation which I would like to run and compare results for different values of gain, as you can see in the image attached.
I can run it very easily for any desired value of gain in the blocks Ka, Kv and Kp and confirm the results.
I now desire to run the simulation programmatically through a for loop to test its response for different values of Ka, Kv and Kp i.e. 0.5, 0.1, 1.5. This is something that I have achieved by running the command ''sim'' and setting my desired values of gain through the command set_param for each of the desired blocks. For the case of changing the gain in Ka for example:
%Create Gain Vector
GVec = [1.5,1,0.5] ;
GName = 'Ka' ;
for i = 1:length(GVec)
%Run Simulation Programmatically
SimOut = sim('DCQ2','ReturnWorkspaceOutputs','on') ;
%Set Gain Parameter Values
%Set initial Value of Parameters Gain
set_param('DCQ2/Kp','Gain','1')
set_param('DCQ2/Kv','Gain','1')
%Set Gain of Block to Change
set_param(strcat('DCQ2/',GName),'Gain', num2str(GVec(i)))
%Extract Outputs
x = SimOut.get('tout') ;
y = SimOut.get('simout') ;
%Plot
hold on
plot(x,y)
grid on
%Legend String
LegStr(i) = {sprintf('Ka Gain: %.1f', GVec(i))} ;
end
%Legend
legend(LegStr,'Location','SE') ;
%Titling & Formatting
title('Electronic Servo Valve Response')
xlabel('Time (s)')
ylabel('Valve Position (m)')
set(gca,'FontSize',14)
The problem with this is that for some reason, somewhere along this process something goes wrong, such that the output figure's plotted lines do not correspond to the correct legend key. This happens in a non-systematic way, which leads me to believe that there might be a problem associated with the whole approach itself rather than the plotting.
In addition when running all 3 gain blocks in the same loop, the outputs are clearly unrealistic. I struggle to find where the interference might be in my code.
Thanks for your help anyway.
KR,
KMT.

Accepted Answer

Nick Choi
Nick Choi on 5 Oct 2017
Edited: Nick Choi on 5 Oct 2017
The reason that the lines do not match with the legend key is because of the order of the code within the loop.
To address the labeling issue, the first half of the code within the for loop should be similar to the this:
for i = 1:length(GVec)
% Set initial gain parameters before simulation
set_param('testing/Kp','Gain','1')
set_param('testing/Kv','Gain','1')
% Set Gain for Ka
set_param(strcat('testing/',GName),'Gain', num2str(GVec(i)))
% Simulate the model with the new gain values
SimOut = sim('testing','ReturnWorkspaceOutputs','on') ;
The 'sim' command will simulate the model with the the 'ReturnWorkspaceOutputs' enabled as described in the following documentation page:
Therefore, the 'sim' command should only be executed once all of the gains have been set to the appropriate values.

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!