interp1 - problem with query points

조회 수: 3 (최근 30일)
Maria
Maria 2019년 8월 9일
댓글: Star Strider 2019년 8월 12일
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?

채택된 답변

Star Strider
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
Maria
Maria 2019년 8월 12일
Thank you! I thought was something like that, so I tried to use the "long" format to see whether there was some last "1" that I could not see, but it did not help.
Star Strider
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by