interp1 - problem with query points
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi,
I am facing a really strange behavior of the function interp1.
I have the variables in the file .mat in attachment, where x is a vector , f is a matrix, and delta is a vector that is computed somewhere else in the code .
From the command windows, I can see that
>> delta =
0.5000 0 0
I am confused because the first value of delta gives me NaN, but not if I simply interpolate directly for 0.5 (what I believe is the value of delta(1) !)
>> f_interp = interp1(x,f,delta(1))
f_interp =
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
>> f_interp = interp1(x,f,0.5)
f_interp =
Columns 1 through 15
0.9882 0.9882 0.9984 1.0221 1.0389 1.0482 1.0843 1.1814 1.2525 1.3288 1.3961 1.5075 1.6173 1.6866 1.7918
Columns 16 through 21
1.8649 1.8835 1.9232 1.9819 2.0135 2.0371
I am assuming that this result has something to do with how the numbers are stored in delta. In fact, if I manually assign
delta(1) = 0.5;
then it works. So this problem has to do with how the number is stored in the vector, and not with the vector itself.
How can I figure out what is wrong with the stored number? Can anyone help?
댓글 수: 0
채택된 답변
Star Strider
2019년 8월 9일
You have encountered ‘floating-point approximation error’. If you subtract ‘delta(1)’ from ‘x’:
Check = x-delta(1)
the result is:
Check =
-5.000000000000001e-01
-4.800000000000001e-01
-4.500000000000001e-01
-4.000000000000001e-01
-3.000000000000001e-01
-1.110223024625157e-16
so you are asking interp1 to extrapolate, without telling it how.
This worked when I tried it:
f_interp = interp1(x,f,delta(1), 'linear','extrap');
and produced a numeric vector with no NaN values.
댓글 수: 2
Star Strider
2019년 8월 12일
As always, my pleasure!
The long format is the correct appproach, however it’s best to see the difference, so using the long format with subtraction provides the necessary information.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!