How to set temporary variables as Gain parameters in Simulink ?

6 次查看(过去 30 天)
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.

采纳的回答

Kaustubha Govind
Kaustubha Govind 2014-6-13
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');
  2 个评论
Pradeep
Pradeep 2014-6-13
Thank you so much Kaustubha Govind. It works !!
I have one more query related to same problem. I need to access the variable 'out' (from Simulink) for every iteration. For Eg:
% start of loop
simOut = sim('Optim_Simulink.mdl', 'SrcWorkspace', 'current');
disp(length(out)); % calculation of overshoot for every iteration
% end of loop
Could you provide me any idea about this ?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by