Error in GWO optimization algorithm
1 次查看(过去 30 天)
显示 更早的评论
Question: 1) writing (Best_pos(1:4)) is right? because it gives me error that the dimensions of the array being concatenated [ParsListMain is 4 elements].
2) Why the Best_pos gives me the ub of the code which is [3.7,0.05,2.8,3.5]?
Thanks in advance.
[Best_score,Best_pos,GWO_cg_curve]= GWO(20,100,[3.6,0.045,2.7,3.4],[3.7,0.05,2.8,3.5],4,@ee_battery_lse);
% Update Battery block with optimized parameters
Pars = reshape([ParsListMain; cellstr(num2str('Best_pos(1:4)'))'],1,[]);
for k=1:2:length(Pars)
evalin('base',[Pars{k} '=' Pars{k+1} ';'])
end
% Display optimized parameters
fprintf(['Optimized parameters for the battery main ' ...
'dialog tab are:\n']);
fprintf('\t%5s = %s\n', Pars{:});
clear i_data v_data t_data T_data Ts
0 个评论
采纳的回答
Walter Roberson
2022-4-24
cellstr(num2str('Best_pos(1:4)'))'
'Best_pos(1:4)' is a character vector. num2str() applied to a character vector gives the character vector. cellstr() applied to the character vector wraps it in a cell array, giving you a 1 x 1 cell array.
You mention that ParsListMain is 1 x 4, but you do not indicate what data type it is. If it were numeric then in context of the [] with the 1 x 1 cell array, then the numeric content would be wrapped in a cell and you would get a 2 x 1 cell array. If, though, ParsListMain is a 1 x 4 cell array then you would be doing vertcat between a 1 x 4 cell and a 1 x 1 cell, and that would be an error.
If Best_pos happens to be a numeric row vector then in
Pars = reshape([ParsListMain; cellstr(num2str(Best_pos(1:4)))'],1,[]);
the indexing Best_pos(1:4) of the (assumed) row vector would give you a 1 x 4 numeric vector, and num2str() of a numeric vector gives you a 1 x whatever character vector, and you would be back to the same problem as before.
If Best_post happens to be any other shape other than row vector (in particular if it happens to be a column vector) then Best_pos(1:4) would give you a 4 x 1 numeric vector, and num2str() of that would give you a 4 x whatever character array, cellstr() of which would give you a 4 x 1 cell array. Which you then transpose to 1 x 4. vertical concatenation of that with a 1 x 4 cell array could work fine.
5 个评论
Walter Roberson
2022-4-25
If ParsListMain is a cell array of character vectors, then ParsListMain(1:4) would be {'Vnom', 'R1', 'AH', 'V1'} which would be a cell array of character vector. It is not clear what the intended output of num2str({'Vnom', 'R1', 'AH', 'V1'}) would be.
Are you looking at https://www.mathworks.com/help/physmod/sps/ug/battery-parameter-extraction-from-data.html ? Because the line there is
Pars0 = reshape([[ParsListMain ParsListDyn ParsListTemp]; cellstr(num2str([InitGuessMain InitGuessDyn InitGuessTemp]'))'],1,[]);
where the Init* variables are each numeric vectors.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Inputs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!