Problem using lsqnonlin with a simulink model

2 ビュー (過去 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 Center および File ExchangeManual Performance Optimization についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by