How to set temporary variables as Gain parameters in Simulink ?

조회 수: 2 (최근 30일)
Pradeep
Pradeep 2014년 6월 13일
댓글: Pradeep 2014년 6월 13일
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 ?
Pradeep
Pradeep 2014년 6월 13일
Is there anything I need to do with DstWorkspace ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by