"Peter Schreiber" <schreiber.peter15@gmail.com> wrote in message <gv3q5q$o5s$1@fred.mathworks.com>...
> Is there any way to rewrite or group the expression in the function below to make it less sensitive to roundoff? 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:(nabs(m))/2
> Rfac=Rfac + (1)^s *factorial(ns)/(factorial(s)*factorial((n+abs(m))/2s)*factorial((nabs(m))/2s))*rho^(n2*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))/2s, and c = (nabs(m))/2s, then ignoring (1)^s and rho^(n2*s) you have
(a+b+c)!/(a!*b!*c!)
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 (ns)! = 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 n2*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 roundoff 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:
http://en.wikipedia.org/wiki/Pascal's_pyramid
Roger Stafford
