why looping just save the last value of iteration

2 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2022-4-18
The objective function of an optimizer must return one scalar cost value - and this value is "lse".

请先登录,再进行评论。

回答(1 个)

Sulaymon Eshkabilov
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.
  1 个评论
Hend Mostafa
Hend Mostafa 2022-4-18
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by