Re: Re: an easy one?
- To: mathgroup at smc.vnet.net
- Subject: [mg88901] Re: Re: an easy one?
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Wed, 21 May 2008 14:47:23 -0400 (EDT)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <g0tr2f$mn5$1@smc.vnet.net> <200805201054.GAA05152@smc.vnet.net>
- Reply-to: murray at math.umass.edu
Two comments about the proffered code
NestWhileList[#+(2-#^2)/(2#)&,1.,Abs[#^2-2]>10^-6&]
for applying Newton's method to approximate Sqrt[2]...
(1) For the function f[x_]:=x^2-2, the formula for Newton's method
simplifies to the well-known "Hero's method", which is what's shown in
the examples of the Mathematica 6 tutorial ApplyingFunctionsRepeatedly:
NestWhileList[(# + 2/#)/2 &, 1., Abs[#^2 - 2] > 10^-15 &]
In general, for Sqrt[c], the function is (# + c/#)/2&, the average of
the current approximation and c divided by the current approximation.
And that's now "intuitively obvious" as a good iterative approximation
method even if you've never heard about derivatives: if x is an
approximation to Sqrt[c], then both the two numbers x and c/x are
approximations to Sqrt[c], so average them to get a better approximation.
(2) Although it makes essentially no difference here, in general it's a
very bad idea when iteratively approximating a root r of f[x]==0 to use
as a stopping criteria that f[#] < epsilon. The trouble is that the
function f may be rather flat near an actual root r, and so the current
approximation x may well satisfy the condition f[x]<epsilon yet be "far"
from r.
In general, a safer stopping criterion is to test the closeness of two
successive approximations to one another (perhaps in conjunction with
the f[#]<epsilon criterion). With Hero's method, for example:
NestWhileList[(# + 2/#)/2 &, 1., Abs[#1 - #2] > 10^-15 &, 2]
dh wrote:
> Hi Francisco,
>
> as you do not give an explicite example, I make one up: calculation of
>
> Sqrt[2] using Newtons method: xnew= xold+ f[xold]/f'[xold] starting w ith
>
> 1.. Where f[x]= x^2-2 and we want an error <10^-6:
>
> NestWhileList[#+(2-#^2)/(2#)&,1.,Abs[#^2-2]>10^-6&]
>
> hope this helps, Daniel
>
>
>
> Francisco Gutierrez wrote:
>
>> I have a code (for clustering) that works well with FixedPointList.
>
>> It takes the following form:
>
>> FixedPointList[veamos[dataset,#]&, prototipos,10]
>
>>
>
>> where veamos is a function that acts on the dataset and prototipos,
>
>> and prototipos is also a list of lists (for example {{5,5},{10,10}} )
>
>>
>
>> But of course using FixedPointList is imperfect. I want to use NestWhileList instead, with a criterion of termination acting on prototipos (i=2Ee., if the absolute value of the substraction of the penultimate prototipos and the last prototipos is less than a certain amount, say 0.01, then stop).
>
>>
>
>> But I haven=B4t been able.
>
>> Can somebody help me?
>
>> Thanks
>
>> Francisco Guti=E9rrez
>
>
>
>
>
>
--
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
- References:
- Re: an easy one?
- From: dh <dh@metrohm.ch>
- Re: an easy one?