Interp1 gives different results in different executions
9 次查看(过去 30 天)
显示 更早的评论
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
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
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
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.
0 个评论
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
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!