Array elements not equal despite only difference being their respective end points
Mostrar comentarios más antiguos
I have two time arrays t1400 and t1500 defined as follows: The only difference between these two arrays is that t1500 has 100000 more cycles than t1400.
t1400=0:1e-3:1400;
t1500=0:1e-3:1500;
I would have assumed that the first 1400001 elements of both arrays would be absolutely equal but alas this is not the case as can be seen from the output of the following code:
equal=ones(length(t1400),1)*36;
for n=1:length(t1400)
equal(n)=(t1400(n)==t1500(n));
end
for n=1:length(equal)
if equal(n)==0
break;
end
end
n
n =
700003
The 700003rd being the first of many positions of the two arrays that are ever so slightly unequal and it's very slight:
>> format long
>> t1400(n)
ans =
7.000020000000000e+02
>> t1500(n)
ans =
7.000020000000001e+02
Unfortunately the system I am modelling is incredibly sensitive and by changing the end time of the time array can lead to very different results. I have accepted this as the case but I was just wondering whether someone could explain to me as to why Matlab gives different values depending upon the end time? Thanks in advance :)
Respuesta aceptada
Más respuestas (1)
dpb
el 17 de Jul. de 2014
1 voto
The "why" of the difference here I can't explain, either. It seems the internals for colon are somehow being generated somewhat like the definition of linspace such that the actual delta step is being computed and that generates a rounding error that eventually accumulates.
Alternatively, it would seem the "deadahead" implementation would interpret the delta from the character constant value in the expression and that should, it would seem as you note, be invariant to the upper bound.
I'd submit this query to TMW Tech Support for an interpretation; it may not be considered a bug but it surely is peculiar. (Not that there's an eventual roundoff error accumulated; only that the two have different as your point is made).
4 comentarios
Peter
el 17 de Jul. de 2014
dpb
el 17 de Jul. de 2014
What do you mean by the "deadahead" implementation?
The for loop with the delta computed from the literal constant converted to internal representation. What it seems must be happening is akin to precomputing the end length and then linspace(first,end,nPoints) wherein the delta would be computed and one could see a rounding difference there in that value whereas it's hard to grok why the literal constant difference doesn't lead to the same result up to the matching length as you noted.
Peter
el 17 de Jul. de 2014
dpb
el 17 de Jul. de 2014
It helps when you have access to the code... :)
Makes sense when one knows how colon is actually implemented, indeed.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!