Interp1 wrong answer

10 views (last 30 days)
Shahzadeario
Shahzadeario on 4 Mar 2016
Hi I am using MATLAB for long time and recently I face very simple line which I can't understand why MATLAB provide this, maybe it is only in my PC. I want to know if you know the reason or it happens in your version as well. I used Interp1 function with input below:
X = [-6.913 0 6.913];
Y = [-9286 0 9286];
xi = -2.612482811810200e-17;
yi = interp1(X,Y,xi)
If you rund it it will give you: yi = 0 which I think it is wrong.
If I run it for xi = 2.612482811810200e-17 the answer will be 3.5093e-14 which should be the same value with negative sign for initial yi.
Thanks in advance
  1 Comment
Roger Stafford
Roger Stafford on 4 Mar 2016
@Shahzadeario: On my ancient version of matlab I get the opposite to your result. For xi = -2.612482811810200e-17 I get yi = -3.5093e-14 and with xi = 2.612482811810200e-17 I get yi = 0. However, you should not worry about such small errors as these. They are differences in rounding errors in computing the linear interpolation, depending on different ways the code happened to be written for 'interp1'. Double precision floating point numbers have 53 bits in their significands (mantissas) giving accuracy of computation to approximately 16 decimal places, and the above error is well within that range.

Sign in to comment.

Answers (1)

Carlos Felipe Rengifo
Carlos Felipe Rengifo on 7 Jul 2018
Instead of the function interp1, you could use this sentence:
yi = polyval(polyfit(X,Y,1),xi)
In Matlab '9.4.0.885841 (R2018a) Update 3' I obtain:
yi =
3.5093e-14

Community Treasure Hunt

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

Start Hunting!