Invalid setting in 'Model' for parameter 'Gain'.

11 views (last 30 days)
SM
SM on 2 Aug 2024
Answered: Paul on 6 Aug 2024
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
SM
SM on 2 Aug 2024
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.
Raghava S N
Raghava S N on 2 Aug 2024
Hi @SM, could you please share the model you are using?

Sign in to comment.

Answers (2)

Divyajyoti Nayak
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!

Paul
Paul on 6 Aug 2024
Hi SM,
Try usin the setVariable method in the Simulink.SimuationInput object.
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

Products

Community Treasure Hunt

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

Start Hunting!