How to set temporary variables as Gain parameters in Simulink ?
2 views (last 30 days)
Show older comments
I am trying to optimize PI controller parameters to reduce Overshoot using fmincon. My model is in Simulink. The Kp and Ti values are changed every iteration of fminsearch and I need to set these values to Gain parameters in Simulink. But, the set_param command sets only a global variable and not a local variable (such as x from fmincon) to the Gain block. Here's my code.
% after all initializations of parameters
[xsim,fvalsim,exitflagsim,outputsim,lambda] = ...
fmincon(@Cost_trap_min,x0,A,b,[],[],[],[],[], options);
% Cost function
function C = Cost_trap_min(x)
% Set gain parameters to Optim_Simulink.mdl file
set_param('Optim_Simulink/Kpvelo', 'Gain', 'x(1)'); % Set Kp to gain block Kpvelo
set_param('Optim_Simulink/Tnvelo', 'Gain', '1/x(2)'); % Set Ti to gain block Tnvelo
sim('Optim_Simulink.mdl');
% calculation of Overshoot and return % Overshoot
C = Overshoot;
end
When I set x(1), which is Kp and x(2), which is Ti to the Simulink, I get the following error.
Error in optimfcnchk/checkfun (line 316)
f = userfcn(x,varargin{:});
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Error using Cost_trap_min (line 20)
Error evaluating parameter 'Gain' in 'Optim_Simulink/Kpvelo'
Error using Cost_trap_min (line 20)
Undefined function 'x' for input arguments of type 'double'.
Error using Cost_trap_min (line 20)
Error evaluating parameter 'Gain' in 'Optim_Simulink/Tnvelo'
Error using Cost_trap_min (line 20)
Undefined function 'x' for input arguments of type 'double'.
Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
When I tried the same set_param by using a variable which is assigned a value before calling the fmincon, it works. But I need to assign the 'x' to Simulink file every iteration. I have also provided the simulink file below. Awaiting for the response.
0 Comments
Accepted Answer
Kaustubha Govind
on 13 Jun 2014
The SIM command looks for variables in the base workspace by default. In this case, 'x' is present in the caller functions workspace, so you need to call SIM as follows:
simOut = sim('Optim_Simulink.mdl', 'SrcWorkspace', 'current');
More Answers (0)
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!