Interp1 gives different results in different executions

조회 수: 4 (최근 30일)
j_solar
j_solar 2014년 7월 11일
편집: dpb 2014년 7월 13일
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
dpb
dpb 2014년 7월 11일
...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 2014년 7월 11일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2014년 7월 12일
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 2014년 7월 12일
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
dpb
dpb 2014년 7월 13일
편집: dpb 2014년 7월 13일
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., ...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by