Interp1 gives different results in different executions

8 views (last 30 days)
I run a program without changing any inputs and I get different results when using interp1. I use the default "linear" mode. I use Matlab 2010a.
Anyone know why?
  4 Comments
dpb
dpb on 11 Jul 2014
...program in which I don't change anything and run it twice and get a different results after an interp1.
The "second-time different" makes me suspect a left-over value from the previous run isn't initialized the same way or is using previous result value as initial value instead of zero, say. This would be particularly likely if it is a script rather than all functions where workspace variables are persistent.
Other possibilities include globals or persistent variables, and the ever-lovable random number that's buried somewhere in a calculation but forgotten...
John D'Errico
John D'Errico on 11 Jul 2014
Interp1 is purely deterministic. There is no random component to it. So SOMETHING is changed in the inputs.
Add a write to a file after each call to interp1. Or use the debugger, stop execution at that point, and use my putvar tool to dump the inputs to the base workspace.
Then verify that the inputs are indeed different to get different output. Verify that even a single bit is different.

Sign in to comment.

Answers (2)

Jan
Jan on 12 Jul 2014
As you find in the comments already:
interp1 replies the same outputs when the input are the same.
So, when you observe different outputs, the inputs are different or the applied test of equality is wrong.

Image Analyst
Image Analyst on 12 Jul 2014
I ran the following code to try to reproduce what you're saying:
clc;
x = 1 : 30;
y = sin(x);
xi = linspace(1, 30, 500);
tic;
for k = 1 : 10000
yi1 = interp1(x, y, xi);
yi2 = interp1(x, y, xi);
if any(yi1~=yi2)
% Something doesn't match
uiwait(warndlg('Mismatch'));
break; % so we can examine further.
end
end
toc
fprintf('Done with demo\n');
It never says mismatch even after running it several hundred thousand times or more.
  1 Comment
dpb
dpb on 13 Jul 2014
Edited: dpb on 13 Jul 2014
Yeah, IA, either OP has uninitialized variables left over or somesuch. Another possibility I suppose would be if is reading external data and the process that creates it causes some slight changes in inputs (like saving ASCII and reading it in w/o full precision--the classic case Lorentz found in his early weather modeling).
Alternatives to those kinds of things revert to such things as flaky memory or the like on the OPs hardware, cosmic rays, etc., etc., ....
Or, I suppose it's in the realm of at least possible if were a multi-processor case that is order-dependent that timing issues could arise that could cause a seemingly random result on occasion.
But, as noted, once gets to interp1 the same input will generate the same output barring the hardware issues, etc., ...

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!