Numerical Analysis: Sections 1.3-1.5

Section 1.3: Algorithms

One might define algorithms as procedures for solving problems. Here’s one from Babylon, circa 2000 BCE....

c=4
f[x_]:=c/x
xmax=2.5*c;
Plot[
f[x]
,{x,0,xmax}
, AxesOrigin->{0,0}
, AspectRatio->1
, PlotRange->{{0,xmax},{0,xmax}}
]

it:=( x = (x + c/x)/2)

Computer problem #1, p. 15:

x=1
it
it
it
it
it
N[%]

The authors encourage us to experiment with lists, and the “%” command:

x=1
{it,it,it,it,it}
N[%]
N[%%,10]

I don’t mind the lists, but the “%” command makes me a little nervous, because if we introduce an intervening command, the meaning of “%” changes -- so this change will propogate through future commands, without another user being able to detect the intentions of the original author. Hence I prefer creating a new variable, with a sensible name, to store the result:

x=1
AppoximateRoots={it,it,it,it,it}
N[AppoximateRoots]
N[AppoximateRoots,20]

In addition to what the authors suggest we do, why don’t we have a look at the convergence in the values?

xs=AppoximateRoots;
ys=c/AppoximateRoots; (* f[xs] *)
Data=Transpose[{xs,ys}]
xmin=Min[Min[xs],Min[ys]]
xmax=Max[Max[xs],Max[ys]]

Plot1=
Plot[
f[x]
,{x,.9*xmin,1.1*xmax}
, AspectRatio->1
, PlotRange->{{xmin,xmax},{xmin,xmax}}
];
Plot2=
ListPlot[
Data
,Filling->Axis
,PlotStyle->{Thick,PointSize[Small]}
];
Show[Plot1,Plot2]