Re: Accuracy problem in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg49102] Re: [mg49061] Accuracy problem in Mathematica
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Thu, 1 Jul 2004 05:25:53 -0400 (EDT)
- References: <200406300934.FAA05317@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
aaaa wrote:
> Hello,
>
> I'm having a problem with a calculation in Mathematica which I can't
> solve. I have an expression which I know to be (from analytical reasons)
> always between 0 and 1. It's a function of a and n ( n being natural and
> a rational) and it looks like this:
>
> 1/(1-a^2)^n +
>
> Sum[((2*n - k - 1)!/((n - 1)!*(n - k)!*2^(2*n - k)))*(1/(1 + a)^k - 1/(1
> - a)^k), {k, 1, n}]
>
>
>
> Let's say a=0.5.
>
> Now, when I try to calculate for small n, it's ok. When calculating for
> large n's (around 400 and above) I'm starting to get wrong results (the
> number not being between 0 and 1). The problem is that the first term
> (the first line before the sum) is VERY VERY close to the negative of
> the second term (the sum), and it's getting closer as n grows. When
> using large n's, Mathematica says they are the same number or even that
> the last term is bigger (which means the whole expression becomes
> negative) - which is wrong. It's a matter of accuracy, and I'm not sure
> how I can fix it.
>
> Can anybody help me?
>
>
>
> Itamar
One way to avoid this cancellation error is to use higher precision in
the input.
f[a_,n_] := 1/(1-a^2)^n + Sum[((2*n - k - 1)!/((n - 1)!*(n - k)!*
2^(2*n - k)))*(1/(1 + a)^k - 1/(1 - a)^k), {k, 1, n}]
Either of the below will suffice. The first is likely to be generally
faster for computation (it avoids costly integer arithmetic that will
arise with large n).
In[7]:= f[N[1/2,100],400]
Out[7]= 0.0561926714876947832018238164192060366422234786
In[8]:= N[f[1/2,400]]
Out[8]= 0.0561927
Daniel Lichtblau
Wolfram Research