필터 지우기
필터 지우기

Problem using lsqnonlin with a simulink model

조회 수: 1 (최근 30일)
Raffaele
Raffaele 2023년 5월 13일
댓글: Raffaele 2023년 5월 13일
Hello, I'm trying to use an lsqnonlin optimization with a simulink model: the simulink model requires as input the variables "sigma_1" and "Km_1", which are the parameters that I want to optimize. since the error function is obtained from the output of the model I inserted all in the function err_fun. The code I used is the following:
p = lsqnonlin(@(p) err_fun(p, acq_data),p0, lb, ub, options);
function err = err_fun(p, acq_data)
sigma_1 = p(1);
Km_1 = p(2);
duration = 0.14;
sim('Simulink_model',[0,duration]);
S_model = spostamento.signals.values;
err = S_model - acq_data;
end
This code gives me an error, which I am almost sure is related to the fact that sigma_1 and Km_1 result undefined for the simulink model. The errore is the following:
Error using Launcher>err_fun (line 83)
Error due to multiple causes.
Error in Launcher>@(p)err_fun(p,acq_data)
Error in lsqnonlin (line 206)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Error in Launcher (line 76)
p = lsqnonlin(@(p) err_fun(p, acq_data),p0, lb, ub, options);
Caused by:
Error using Launcher>err_fun (line 83)
Error evaluating parameter 'stiff_up' in 'VESevo_sim_V1/Translational Hard Stop1'
Error using Launcher>err_fun (line 83)
Undefined function or variable 'Km_1'.
Error using Launcher>err_fun (line 83)
Variable 'Km_1' has been deleted from base workspace.
Suggested Actions:
Undo the deletion. - Fix
Load a file into base workspace. - Fix
Create a new variable. - Fix
Error using Launcher>err_fun (line 83)
Error evaluating parameter 'D_up' in 'VESevo_sim_V1/Translational Hard Stop1'
Error using Launcher>err_fun (line 83)
Undefined function or variable 'sigma_1'.
Error using Launcher>err_fun (line 83)
Variable 'sigma_1' has been deleted from base workspace.
Suggested Actions:
Undo the deletion. - Fix
Load a file into base workspace. - Fix
Create a new variable. - Fix
Failure in initial objective function evaluation. LSQNONLIN cannot continue.
I don't understand why the two parameters results undefined since this method of using a function to calculate the error and give the function as input to the lsqnonlin, works in other context without a simulink model.
Do you know how to solve this problem? Is it right that the errorrs are caused by sigma_1 and Km_1? Maybe with a simulink model this is not the right way to use lsqnonlin?
Thank you!

채택된 답변

Torsten
Torsten 2023년 5월 13일
이동: Torsten 2023년 5월 13일
  댓글 수: 3
Torsten
Torsten 2023년 5월 13일
I have no experience with Simulink, but input of parameters seems to work via
in = in.setVariable('Kp',pid(1),'Workspace',mdl);
in = in.setVariable('Ki',pid(2),'Workspace',mdl);
in = in.setVariable('Kd',pid(3),'Workspace',mdl);
doesn't it ?
So it seems natural to set
in = in.setVariable('sigma_1',p(1),'Workspace',mdl);
in = in.setVariable('Km_1',p(2),'Workspace',mdl);
in your case.
Raffaele
Raffaele 2023년 5월 13일
Yes, sorry this is my first time using simulink. It actually worked. Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Design Optimization에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by