why looping just save the last value of iteration

조회 수: 2 (최근 30일)
Hend Mostafa
Hend Mostafa 2022년 4월 18일
댓글: Hend Mostafa 2022년 4월 18일
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
Sulaymon Eshkabilov 2022년 4월 18일
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