why looping just save the last value of iteration
1 view (last 30 days)
Show older comments
how to get all values of lse not only the last iteration?
function lse = ee_battery_lse_r(params)
% Cost function used by ee_battery_opt_m.m
% Copyright 2019 The MathWorks, Inc.
% Calculate and return sum of squares of the differences
% load ee_battery_data.mat
% ParsListMain = {'Vnom', 'R1', 'AH', 'V1', 'AH1'};
% assignin('base','ParsList',ParsListMain(1:4));
ParsList = evalin('base', 'ParsList');
Pars = reshape([ParsList; cellstr(num2str(params'))'],1,[]);
battery_data = evalin('base', 'battery_data');
v_data = evalin('base', 'v_data');
idx_data = evalin('base', 'idx_data');
Model = evalin('base', 'Model');
lse = 0;
warning off
for idx = idx_data
assignin('base', 'idx_data', idx);
% set_param([Model '/Battery'], Pars{:})
for k=1:2:length(Pars)
if regexp(Pars{k},'charge')
evalin('base',['AH0=' Pars{k+1} ';'])
else
evalin('base',[Pars{k} '=' Pars{k+1} ';'])
end
end
if ~max(contains(Pars,'charge')) && ~max(contains(Pars,'tau1'))
AH = evalin('base', 'AH');
assignin('base','AH0',AH*battery_data(idx).SOC0);
end
% Catch invalid parameters supplied by fminsearch
try
out = sim(Model);
% Difference between block current output and data
data_diff = out.Vo.signals.values-v_data;
lse = lse + (data_diff'*data_diff);
catch
% For invalid parameters return a large function error
lse = 1e5;
end
end
warning on
end
1 Comment
Torsten
on 18 Apr 2022
The objective function of an optimizer must return one scalar cost value - and this value is "lse".
Answers (1)
Sulaymon Eshkabilov
on 18 Apr 2022
Because the indexes are missing, e.g.:
data_diff(??) = out.Vo.signals.values-v_data;
lse(??) = lse(??) + (data_diff'*data_diff);
Assign indexes to your output variables according to your set [for .. end] loop.
See Also
Categories
Find more on Quadratic Programming and Cone Programming 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!