Interp1 gives different results in different executions
Ältere Kommentare anzeigen
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 Kommentare
John D'Errico
am 11 Jul. 2014
You ARE changing the inputs. You just don't know it.
If you are positive that this has happened, then why not give an example where it happens?
Odds are you THINK the numbers are the same, but due to floating point issues, they are not.
j_solar
am 11 Jul. 2014
dpb
am 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
am 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.
Antworten (2)
Jan
am 12 Jul. 2014
0 Stimmen
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
am 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 Kommentar
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., ...
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!