# why looping just save the last value of iteration

5 views (last 30 days)
Hend Mostafa on 18 Apr 2022
Commented: Hend Mostafa on 18 Apr 2022
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
% 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
Torsten on 18 Apr 2022
The objective function of an optimizer must return one scalar cost value - and this value is "lse".

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);
Hend Mostafa on 18 Apr 2022
i removed the indexes so that I can see the results in command window, but also after adding them, only the last value of lse is taken

### Categories

Find more on Buffers, Switches, and Counters in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!