Clear Filters
Clear Filters

interp1 - problem with query points

4 views (last 30 days)
Maria
Maria on 9 Aug 2019
Commented: Star Strider on 12 Aug 2019
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?

Accepted Answer

Star Strider
Star Strider on 9 Aug 2019
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 Comments
Maria
Maria on 12 Aug 2019
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 on 12 Aug 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!