3 "for" loops optimization or changing for global maximum finding
Mostrar comentarios más antiguos
Hi,
I have a function (function1), which has three variables (i,j,k) which I need to change from -20 to 20 in step 1, and later with function2, I have to find maximum DB number and place (ci, cj, ck), where DB is the highest (it can be everyhere, depending on initial data file).
This code works good, but the main disadvantage of it is time. Calculations take about 3 hours (a lot of combinations ofc.). I need somehow to optimise my code. Can someone explain how can I find global maximum of DB faster, than doing 3 "for" loops?
I have heard about some algorithms which works, but I don't know how and how to use it in matlab.
Thanks.
max = 0;
ci = 0;
cj = 0;
ck = 0;
% kr1 = 0;
% kg2 = 0;
% kb3 = 0;
for i = -20:1:20
for j = -20:1:20
for k = -20:1:20
[spd, kr, kg, kb] = function1( i, j, k, A, B, C );
DB = function2( mspd4, mspd5, Xn, Yn, Zn, U, range, D, E, F, spd );
if(max<=DB)
max = DB;
ci = i; cj = j; ck = k;
% kr1 = kr; kg2 = kg; kb3 = kb;
end
end
end
end
6 comentarios
Michael Croucher
el 5 de Oct. de 2020
Can you share function1 and function2 to make this an example that's runnable please?
Domantas B
el 6 de Oct. de 2020
Domantas B
el 6 de Oct. de 2020
Walter Roberson
el 6 de Oct. de 2020
[spd, kr, kg, kb] = function1( i, j, k, A, B, C );
DB = function2( mspd4, mspd5, Xn, Yn, Zn, U, range, D, E, F, spd );
Most of those variables are not defined. A, B, C, mspd4, mspd5, Xn, Yn, Zn, U, range, D, E, F are all not defined at that point.
In order to have a hope of optimizing your code, we need to be able to run your code.
You should also indicate which of the variables are always going to be the same for this purpose, and which you might change in the future.
Inside function1:
spektras1 = mspd1; %initial file 1
spektras2 = mspd2; %iniitial file 2
spektras3 = mspd3; %initial file 3
None of those mspd* variables are defined.
Domantas B
el 6 de Oct. de 2020
Editada: Domantas B
el 6 de Oct. de 2020
Domantas B
el 6 de Oct. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Surrogate Optimization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!