Results inside for are different from outside
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Henrik Jacobsen
le 1 Nov 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!