Also, I would like to add that after I run the code, it probably stops after one iteration where only only one set of values of Kp, Ki, Kd are shown and in the Simulink, it shows that Variable 'Kp' (or Ki or Kd) has been deleted from base workspace.
Invalid setting in 'Model' for parameter 'Gain'.
9 views (last 30 days)
Show older comments
I am trying to tune a PID controller for controlling a plant model buit in Simulink. My aim is to obtain the gains of the PID controller using an optimization technique whose code is written in a script file. So, the objective function is calculated by obtaining the loged values from the simulink model. While I run my code, it always shows the error: Invalid setting in 'Model/PID/Gain' for parameter 'Gain', where the simulink file's name is Model. Since I am tuning the gains using optimization, the are changing in each iteration. Kp, Ki, and Kd are the gains. The error is: Invalid setting in 'Model/PID/Gain' for parameter 'Gain'.
function objective_func = F3(x)
Kp=x(1)
Ki=x(2)
Kd=x(3)
mdl = "Model";
open_system(mdl)
in = Simulink.SimulationInput(mdl)
out = sim(in)
simout = out.J(3001) %objective function value
objective_func=simout;
end
2 Comments
Answers (2)
Divyajyoti Nayak
on 6 Aug 2024
Hi @SM, from my understanding you are calling the ‘F3’ function iteratively and based on the output from the function changing the gains and passing them in the next call of the ‘F3’ function. Since, the input to the function is a vector, I am also assuming that outside the function the gains are stored in a vector and this could be the source of your issue. Simulink cannot access variables that are used inside functions as they are part of the function workspace not the base workspace. To fix this you can move the definitions of ‘Kp’,’Kd’ and ‘Ki’ from the function to before the function call in the loop. This will populate these values in the base workspace for the Simulink model to use.
gains = [1, 1, 1];
for i = 1:10 %dummy loop
%calculate gains
%Define Kp, Kd, Ki for model to use
Kp=gains(1);
Ki=gains(2);
Kd=gains(3);
%Call F3 to get objective function
objective_func = F3();
end
function objective_func = F3()
mdl = "Model";
open_system(mdl)
in = Simulink.SimulationInput(mdl)
out = sim(in)
simout = out.J(3001) %objective function value
objective_func=simout;
end
Hope this helps!
0 Comments
Paul
on 6 Aug 2024
Hi SM,
function objective_func = F3(x)
mdl = "Model";
open_system(mdl)
in = Simulink.SimulationInput(mdl)
in = in.SetVariable('Kp',x(1));
in = in.SetVariable('Ki',x(2));
in = in.SetVariable('Kd',x(3));
out = sim(in)
simout = out.J(3001) %objective function value
objective_func=simout;
end
0 Comments
See Also
Categories
Find more on PID Controller Tuning 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!