"lily" wrote in message <jqn6jr$gdp$1@newscl01ah.mathworks.com>...
> I found the following situation, combined "diag" and "sum" commands will cause accumulation error. Then how to eliminate this condition? Thanks.
>
> B=1e+10*rand(25);
> B=Bdiag(diag(B));
> sumB=sum(B);
> C=B;
> C=C+diag(sum(B));
> sumC=sum(C);
>
> err=sumC(abs(sumC)>1e5); %what I need is the matrix C, sum(C) should be all zeros.
> tf1=any(diag(B)); %tf1=0 means code B=Bdiag(diag(B)) produces no error.
> tf2=any(sumB~=sum(B)); %tf2=0 means "sum" produces no error.
>
> >>err = % how to eliminate accumulation error?
> 1.0e004 *
> 0.1144 0.1907
> >>tf1 = 0
> >>tf2 = 0
Welcome to the world of floating point arithmetic.
You can't eliminate all such errors. Not possible here.
In fact, the order in which you perform even simple
addition and subtraction operations will affect your
result when done in floating point arithmetic.
Instead, learn to work with it. Learn to make your
computations insensitive to such problems. Classes
and books on numerical methods teach you just
that.
John
