Results inside for are different from outside
Mostrar comentarios más antiguos
Hi,
I have implemented an optimization algorithm. Now I am trying to understand which are the value of some parameters that make the error the smallest possible. Therefore I created a function that receives three files and the value of the parameter I am varying. All the other parameters are mantained fixed.
So whenever I do this:
J=GradMomChemo_TESTE('Parametros_L2_JN','Controlo_5_tudo','Cond_Iniciais_1',1.2);
the result is:
J=1.2708*10^7
However, if I calculate this inside a for the result is different:
up=1.1:0.05:2;
mini=zeros(2,length(up));
for i=1:length(up)
mini(1,i)=up(i);
mini(2,i)=GradMomChemo_TESTE('Parametros_L2_JN','Controlo_5_tudo','Cond_Iniciais_1',up(i));
end
In this case the value of J correpondent to up=1.2 is:
J=2.712*10^7
Why does this happen? What can I do to change this situation?
Respuesta aceptada
Más respuestas (1)
Henrik Jacobsen
el 1 de Nov. de 2017
I have two guesses. It's hard to know without having the functions.
1) You are not actually looking at up=1.2 in your for loop.
2) You have some global variable that changes when running the for loop. To test both cases, try running
up=[1.2 1.1 1.2];
mini=zeros(2,length(up));
for i=1:length(up)
mini(1,i)=up(i);
mini(2,i)=GradMomChemo_TESTE('Parametros_L2_JN','Controlo_5_tudo','Cond_Iniciais_1',up(i));
end
if mini(2,1) is different from mini(2,3), then there is a variable that changes when the function is running. If they are the same and equal to 1.2708*10^7, then you were not looking at the value of up you thought you were.
1 comentario
Elisa Pacheco
el 1 de Nov. de 2017
Categorías
Más información sobre Matrices and Arrays 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!