When i am trying to simulate my sine approximation in matlab i found a strange problem. The problem is that when i apply my function to an array, it returns one results, whereas applying functions to individual values gives a slightly different result.
I was able to get same behaviour in this example:
z = single(0:0.001:1);
F = @(x) (x.^2 - single(1.2342320e-001)).*x.^2; %some test function
z(999) % Returns 9.9800003e-001
F(z(999)) % Returns 8.6909407e-001
temp = F(z);
temp(999) % Voila! It returns 8.6909401e-001
Also I found a few things. One is that first result is right (not the latter). Second, is that rearrangement of terms sometimes solves the problem. So i have no idea how to get rid of that.
Using single-precision numbers, both results are "equal" (difference is smaller than the relative accuracy of the
singletype). The following statement evaluates to true:EDIT
If you really want to have control over this, you can try disabling the MATLAB accelerator to force it to use the same execution paths for both regular and vectorized code:
The result for the first/second respectively:
Obviously, by default both the accelerator and the just-in-time compiler are turned on.