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 Center および File ExchangeProgrammatic Model Editing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by