Problem using lsqnonlin with a simulink model
1 view (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Torsten
on 13 May 2023
Moved: Torsten
on 13 May 2023
Did you study this example:
?
3 Comments
Torsten
on 13 May 2023
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.
More Answers (0)
See Also
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!