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!