Results inside for are different from outside

13 views (last 30 days)
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?

Accepted Answer

Cam Salzberger
Cam Salzberger on 1 Nov 2017
Hello Elisa,
There are a couple of possible reasons, but without the CradMomChemo_TESTE function, it's difficult to tell which might be the root cause. One possible cause, though, is precision difficulties. If your function is sensitive to very small perturbations, then even simple precision differences can cause drastically different results.
Technically, neither of your pieces of code are calling that function with 1.2 exactly. That is because 1.2 cannot be represented exactly in double precision. In the first snippet, this is the value you call with:
fprintf('%.20f\n',1.2)
1.19999999999999995559
In the next snippet, this is the value:
x = 1.1:0.05:2;
fprintf('%.20f\n',x(3))
1.20000000000000017764
Here is some information on floating point accuracy. There have also been several questions here on MATLAB Answers that address this.
If you run your function outside the loop with the exact same value x(3), and it is still giving different results, then it is likely that something different is happening with the function itself. It's possible that there are two versions of the function on your path, and the code calls one while your test from the Command Window calls the other. Check for duplicate copies with which -all.
It's also possible that your function has some time, parent, or otherwise random properties to it. Put a breakpoint in at the entrance to the function and step through it, to see where differences start to happen.
Hope this helps!
-Cam
  2 Comments
Elisa Pacheco
Elisa Pacheco on 1 Nov 2017
This is exactly the problem! I tried the vector
up=[1 1.05 1.1 1.15 1.2 (...) 2]
and everything went right! thank you soo much!
Cam Salzberger
Cam Salzberger on 1 Nov 2017
Sure thing. I would have a look at the function that is using these numbers though. If it's varying by as much as 50% from an input change of about 0.0000000000001%, then your function is either extremely sensitive to input changes, or your function is unstable about the input point of 1.2.
Does that function use any conditionals of the form:
if inputVal < 1.2
...
or anything like that? Or does it compare equality with 1.2 exactly at some point? If so, you may either want to ensure that the input at that point is exactly what you expect it to be, or you might consider adding some tolerance terms. For example:
if abs(inputVal-1.2) < 1e-7
...
Or
if inputVal >= 1.2-1e-7
...

Sign in to comment.

More Answers (1)

Henrik Jacobsen
Henrik Jacobsen on 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.
  1 Comment
Elisa Pacheco
Elisa Pacheco on 1 Nov 2017
Thank you Henrik! Your first guess was right. In fact, when I use
up=[1.2 1.1 1.2]
I obtain 1.2708*10^7! Thank you for your help!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!