File: iterate.en

package info (click to toggle)
euler 1.61.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 5,164 kB
  • sloc: ansic: 24,761; sh: 8,314; makefile: 141; cpp: 47; php: 1
file content (37 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
% This notebook demonstrates fixpoint iteration.
>function f(x)
$return (x+2/x)/2
$endfunction
>longformat;
% The iteration x(n+1)=f(x(n)) converges quickly to
% sqrt(2).
>iterate("f",2)
% It might be easier to use an expression in x instead
% of a function.
>iterate("(x+2/x)/2",2)
% niterate returns the history of 5 iterations.
>niterate("f",2,5)
% Starting with an interval does not work.
>niterate("f",~1,2~,5)
% We must use the interval Newton operator.
>function f1(x)
$m=middle(x);
$return m-(m^2-2)/(2*x)
$endfunction
% This converges quickly to an inclusion iof the zero.
>iterate("f1",~1,2~)
% iterate works in several dimensions.
>function g(x)
$return [(x[1]+x[2])/2,sqrt(x[1]*x[2])]
$endfunction
% The iterations converge to the arithmetic geometric
% mean of the start values.
>iterate("g",[1,2])
% The convergence is rather quick.
>niterate("g",[1,2],4)
% We can use an Interval iteration. This shows us that
% the iteration is stable. The n-th iterate is within
% the given bounds. It does not prove, that the limit
% is in these bounds, however.
>y=niterate("g",[~1~,~2~],4)
>