From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: round-off
Date: Fri, 22 May 2009 07:29:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 37
Message-ID: <gv5k7t$r10$>
References: <gv3q5q$o5s$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: 1242977341 27680 (22 May 2009 07:29:01 GMT)
NNTP-Posting-Date: Fri, 22 May 2009 07:29:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:541723

"Peter Schreiber" <> wrote in message <gv3q5q$o5s$>...
> Is there any way to re-write or group the expression in the function below to make it less sensitive to round-off? Does it help any to break the large expression into 
> smaller pieces? I suppose matlab already uses double precision. Is there any way to increase the precision beyond that?
> .......
> Rfac=0;
>          for s=0:(n-abs(m))/2
>               Rfac=Rfac + (-1)^s *factorial(n-s)/(factorial(s)*factorial((n+abs(m))/2-s)*factorial((n-abs(m))/2-s))*rho^(n-2*s);
>          end
> .......

  It is not possible to give a precise answer to your question, Peter, without knowing what values you contemplate for n, m, and rho.  However, perhaps the following will be of some assistance to you.

  You are computing the sum of a finite number of terms in a polynomial involving powers of rho.  (I assume here that n+abs(m) is even, since your expression would not make sense otherwise.)  The coefficients of that polynomial are each certain terms that, except for the alternating signs, occur in the trinomial expansion of a power of the sum of three quantities.  Specifically in your case if we let a = s, b = (n+abs(m))/2-s, and c = (n-abs(m))/2-s, then ignoring (-1)^s and rho^(n-2*s) you have


This is the number of ways a+b+c objects can be divided up with a of them in one box, b in another box, and c in a third box and it is always an exact integer.

  I am guessing that you will also want to compute such integers exactly.  Up until these quantities exceed 2^53, you can presumably use the nchoosek function to make this computation accurately according to the formula

 (a+b+c)!/(a!*b!*c!) = (a+b+c)/(a!*(b+c)!) * (b+c)!/(b!*c!)

where each of these two factors can come from nchoosek using

 nchoosek(a+b+c,a) * nchoosek(b+c,b)

For a given value of a+b+c the maximum value of this occurs when a, b, and c are approximately equal.  In your case that would correspond to m = 0 and n being approximately four times the size of s.  I would therefore guess that the calculation using this method would first run into accuracy problems with n = 52, m = 0, and s = 13 or thereabouts, and for the very good reason that higher values of the trinomial coefficients would be too large to be precisely represented in matlab's double precision.

  As you can well imagine, using this method will allow one to avoid the inaccuracies of computing directly with the factorial function.  For example, with the above value for n, m, and s, you would encounter (n-s)! = factorial(39) in your original expression, which is far, far beyond matlab's double precision capabilities for exact integers.

  As for carrying out the rest of your desired computation, a lot would depend on the magnitude of rho.  With rho substantially greater than one, the early terms with n-2*s powers of rho would tend to be larger than the later terms, but the reverse is true if rho is less than one.  In any case I would favor an ordering of addition/subtraction which proceeds from the smaller magnitudes and ends with the larger ones as a means of best reducing accumulating round-off error.  You could accomplish that by sorting the term values in accordance with ascending magnitudes and then carry out the addition of those terms in the corresponding ordering indicated by the sort function.

  It may be of at least theoretical interest that the above trinomial coefficients occur in what is known as a Pascal Pyramid, and in your summation you are using the trinomial coefficients along a certain line within that pyramid as s varies.  In fact these coefficients could actually be computed by simple addition (admittedly quite a lot of additions) within the pyramid using a method analogous to the generation of binomial coefficients using a Pascal Triangle.  For more information on this see the Wikipedia website at:'s_pyramid

Roger Stafford