Domanda

I'm using the code below to check whether X and Y are giving me the same results for each iteration. Essentially, X and Y (1 x 16 Vectors) are only slightly different and give the value for an equation which has variable values that are chosen via random number generators. When I set a Breakpoint in the Nielsennewupadated() function at the equation and run the code below I get that that the values from the equation in Nielsennewupadated() and Nielsennew() are exactly the same. However, when I run the code without a Breakpoint the values associated with the two functions diverge. I am a bit confused about how this could occur. Thanks.

no_iterations = 1;
casechoice = 1;

for i=1:10
%   X = MCsolution(no_iterations)
%   Y = MCsolutionupdated(no_iterations)
  X = Nielsennew(casechoice)
  Y = Nielsennewupdated(casechoice, no_iterations)
  if (X(1,1)~=Y(1,1))
    fprintf('Iter %i disagrees by %g\n',i,X(1,1)-Y(1,1));
  end
end
È stato utile?

Soluzione

As values are floating point numbers, they have a finite precision. You should compare their difference against some threshold value

if (abs(X(1,1) - Y(1,1)) > 1e-10)
  fprintf('Iter %i disagrees by %g\n', i, X(1,1) - Y(1,1));
end

here 1e-10 is a threshold.

Consider the following example

> X = 0.037900
X = 0.037900
> Y = exp(log(X))
Y = 0.037900
> X -Y 
ans =  1.3878e-17

Two numbers X and Y look the same, but their decimal notation start to differ from 17th digit.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top