why looping just save the last value of iteration

2 visualizaciones (últimos 30 días)
Hend Mostafa
Hend Mostafa el 18 de Abr. de 2022
Comentada: Hend Mostafa el 18 de Abr. de 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
% 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 comentario
Torsten
Torsten el 18 de Abr. de 2022
The objective function of an optimizer must return one scalar cost value - and this value is "lse".

Iniciar sesión para comentar.

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 18 de Abr. de 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.
  1 comentario
Hend Mostafa
Hend Mostafa el 18 de Abr. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by