precision problem when creating a time vector

Hello!
Ia have come across a very strange phenomena:
When you create the following vectors (time vectors) there seems to be a precision problem, allthough I don't understand why:
t1 = 0:.00001:0.0010; %time vector for a pulse 10µs step
t2 = 0.00101:.00001:0.015; % time vector for a discharge 10µs step
t3 = [t1, t2]; %resulting time vector for plot
Normally, I would expect, that if you try to find the index of t3 at the time of 4.5ms (0.0045) this should be no problem. But if you run the code
find(t3 == 0.0045)
ans = 1×0 empty double row vector
you will get no return, as for some reason matlab is not hitting the exact 4.5ms in the vector.
Furthermore if you try to find the deviation by the following code line:
t3(451) - 0.0045 % 451 is the index for 4.5ms
ans = 8.6736e-19
you can see that there is a very small deviation from the exact value. But this should normally not be the case. At least for my understanding. Or where is the mistake here?
kind regards
Benjamin

1 个评论

@Stephen23: Please move this to the section of answers.
@Benjamin Brammer: "Or where is the mistake here?" Follow Stephen's links. You observe the expected behavior of floating point arithmetics with a limited precisions. This is explain in the first lesson of courses for numerical maths and you find it on the first pages of text books for this topic.
"Why is 0.1 + 0.2 ~= 0.3" is a frequently asked question. Welcome to the world of numerical maths.

请先登录,再进行评论。

 采纳的回答

Stephen23
Stephen23 2022-11-29
移动:Cris LaPierre 2022-11-29

7 个评论

@Stephen23 @Jan: Thanks for your answers and the additional information. So it is inherent because of the floating point number expression that - even when equally spaced with 10µs - I cannot hit the 4.5ms, correct?
Two different ways of calculating the "same" value can accumulate different floating point error. That is entirely expected.
"even when equally spaced with 10µs"
10us cannot be stored exactly, so what does "equally spaced" with it really mean?
fprintf('%.64f\n',0.00001)
0.0000100000000000000008180305391403130954586231382563710212707520
@Stephen23: Ah, ok. Thanks again for the clarification. So the biggest question would be: How do I get then values of i.e. an amplitude vector at certain time points, when I can't hit the correct point?
So something like amplitude(t3 >= 0.0045 & t3 < 0.0045) ?
Two approaches that would work:
  • actually work integer/whole numbers (e.g microseconds), rather than fractions of a second.
  • compare the absolute difference of floating point values against a tolerance:
abs(A-B)<tol
The tolerance depends on your measurement accuracy and is limited by the floating point precision of A and B.
Ok. I will try and experiment wiht your suggestions. Thanks again!
The ismembertol function may also be of use.
@Steven Lord thanks for your suggestion, I will check it.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by