       Re: Coaxing N[] to work

• To: mathgroup at smc.vnet.net
• Subject: [mg73394] Re: Coaxing N[] to work
• From: Scott Hemphill <hemphill at hemphills.net>
• Date: Thu, 15 Feb 2007 05:01:40 -0500 (EST)
• References: <equo6r\$in0\$1@smc.vnet.net>

```<p at dirac.org> writes:

> Sometimes N[,] doesn't appear to work.  Like here:
>
>
> x = {2.0, 3.0, 5.0};
> A = { {6.0, 2.0, 1.0}, {2.0, 3.0, 1.0}, {1.0, 1.0, 1.0} };
> For[ k=0, k<15, ++k,
>    lambda = x.A.x/(x.x);
>    y = LinearSolve[A,x];
>    x = y / Norm[y,Infinity];
> ]
> N[lambda, 30]
>
>
> The output is:
>
>    Out= 0.578933
>
> I was expecting 30 digits.  Why did N[] ignore my request for 30 digits?

Because lambda had already been calculated, and it didn't have 30 digits.

See how Mathematica prints the following numbers:

In:= 2

Out= 2

In:= 2.0

Out= 2.

In:= 2`30

Out= 2.00000000000000000000000000000

Now see what the precision of each number is:

In:= Precision

Out= Infinity

In:= Precision[2.0]

Out= MachinePrecision

In:= Precision[2`30]

Out= 30.

Here's your problem, redone with infinite precision:

In:= x = {2, 3, 5};

In:= A = { {6, 2, 1}, {2, 3, 1}, {1, 1, 1} };

In:= For[ k=0, k<15, ++k,
lambda = x.A.x/(x.x);
y = LinearSolve[A,x];
x = y / Norm[y,Infinity];
]

In:= lambda

1102158619423970036337
Out= ----------------------
1903774504398915184457

In:= N[lambda, 30]

Out= 0.578933385691052787623495851172

Here's your problem, redone with specified precision.  Whenever a number
with infinite precision is arithmetically combined with a number with
finite precision, the result has finite precision.  If I had wanted, I
could have set all the elements in x and A to finite precision by using
SetPrecision, e.g., "x = SetPrecision[ {2, 3, 5}, 30 ];".

In:= x = {2`30, 3, 5};

In:= A = { {6, 2, 1}, {2, 3, 1}, {1, 1, 1} };

In:= For[ k=0, k<15, ++k,
lambda = x.A.x/(x.x);
y = LinearSolve[A,x];
x = y / Norm[y,Infinity];
]

In:= lambda

Out= 0.5789333856910527876234959

In:= Precision[%]

Out= 24.9696

Note that some precision was lost due to round off error.  You can't
get more precision after the number has already been calculated:

In:= N[%15,100]

Out= 0.5789333856910527876234959

Scott
--
Scott Hemphill	hemphill at alumni.caltech.edu
"This isn't flying.  This is falling, with style."  -- Buzz Lightyear

```

• Prev by Date: Re: Controlling display of frames in a movie
• Next by Date: Re: MathPlayerSetup installation
• Previous by thread: Re: Coaxing N[] to work
• Next by thread: Re: Coaxing N[] to work