>> x = 1; >> x = [x,x(end)+eps(x(end))]; >> x = [x,x(end)+eps(x(end))]; >> x = [x,x(end)+eps(x(end))]; >> x = [x,x(end)+eps(x(end))]; >> diff(x) > 0 ans = 1 1 1 1 >> x1 = x + eps(x)/2; >> x1 - x ans = 0 2.2204e-16 0 2.2204e-16 0]]>

Shouldn’t the above expression evaluate to zero instead of 2.2204e-16 as a number smaller the eps (sqrt(2) is being added to sqrt(2) ?

Thanks

]]>F = @(x,y)exp(10*x).*tan(y)

Fpeek = @(x,y)peek(F(x,y),x,y)

Q1 = integral2(Fpeek,0,1,0,pi/4)

[Fe,Xe,Ye] = peek()

shows that integral2 used 6 function calls on mesh grids of size 14×14, 2×3, 14×14 etc. in R2013b, and you can plot the mesh grids coloured according to their iteration number with:

figure;view(3);colorbar;hold all;

cellfun(@surf,Xe,Ye,Fe,arrayfun(@(k)repmat(k,size(Fe{k})),1:numel(Fe),’Unif’,0)’);

Ben

]]>You should see “?” mark next to “Write your comment:” which shows the markup hints that we had before. Then you could more fully format your comments.

–Loren

]]>I usually rewrite my loop counter differently if length is important. Something more like:

xi = xmin + (xmax-xmin)*(1:totalNum)/totalNum

Then I know the number of elements for sure.

–Loren

]]>me again. It is strange that the preview of the comment is so well formatted but the final, published version looks so poor.

Sorry for that, but where is the nice format gone?

Alexander

]]>thanks for this short overview on the issue. Incidentally I came along the same issue in distributive law:

Since

(22/1000-17/1000) – (22-17)/1000 = -2.602085213965211e-018

one always has to be careful as discussed. But for me, the issue was quite more involved. Consider the situation, where you are building vectors out of the calculation results:

[0:0.0005:(22/1000-17/1000)], [0:0.0005:(22-17)/1000]

From this you see, that the results do have different lengths:

ans =

0 0.0005 0.0010 0.0015 0.0020 0.0025 0.0030 0.0035 0.0040 0.0045

ans =

0 0.0005 0.0010 0.0015 0.0020 0.0025 0.0030 0.0035 0.0040 0.0045 **0.0050**

That is not to nice and finally, when putting to an outer loop like

for m = 18:22

array1(m-17,:) = [0:0.0005:(m-(m-2))/1000]

array2(m-17,:) = [0:0.0005:(m/1000-(m-2)/1000)]

end

you end up with an unexpected error

Subscripted assignment dimension mismatch.

@ m = 20 since array2 so far has 4 columns but suddenly gets 5 columns, the number of columns for array1.

This might be / is very annoying.

To conclude: I currently guarantee my precision by using

round((m/1000-(m-2)/1000)*1000)/1000

but would appreciate better solutions.

Thanks for your very nice and helpful blog.

Alexander

]]>