Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Steffensens-method/00-META.yaml
Normal file
3
Task/Steffensens-method/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Steffensen's_method
|
||||
note: Mathematics
|
||||
105
Task/Steffensens-method/00-TASK.txt
Normal file
105
Task/Steffensens-method/00-TASK.txt
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
[[wp:Steffensen's_method|Steffensen's method]] is a numerical method to find the roots of functions. It is similar to [[wp:Newton's_method|Newton's method]], but, unlike Newton's, does not require derivatives. Like Newton's, however, it is prone towards not finding a solution at all.
|
||||
|
||||
In this task we will do a root-finding problem that illustrates both the advantage—that no derivatives are required—and the disadvantage—that the method often gives no answer. We will try to find intersection points of two [[wp:Bézier_curve|Bézier curves]].
|
||||
|
||||
===Steffensen's method===
|
||||
|
||||
We will be using the variant of Steffensen's method that employs [[wp:Aitken's_delta-squared_process|Aitken's extrapolation]]. Aitken's extrapolation is illustrated by the following [[ATS]] function:
|
||||
|
||||
fun aitken
|
||||
(f : double -> double, (* function double to double *)
|
||||
p0 : double) (* initial fixed point estimate *)
|
||||
: double =
|
||||
let
|
||||
val p1 = f(p0)
|
||||
val p2 = f(p1)
|
||||
val p1m0 = p1 - p0
|
||||
in
|
||||
p0 - (p1m0 * p1m0) / (p2 - (2.0 * p1) + p0)
|
||||
end
|
||||
|
||||
The return value is a function of <math>p0</math>, <math>p1=f(p0)</math>, and <math>p2=f(f(p0))</math>. What one is trying to find is a so-called [[wp:Fixed_point_(mathematics)|''fixed point'']] of <math>f</math>: a value of <math>p</math> such that <math>f(p) = p</math>. Our implementation of Steffensen's method will be to repeat Aitken's extrapolation until either a tolerance is satisfied or too many iterations have been executed:
|
||||
|
||||
fun steffensen_aitken (* finds fixed point p such that f(p) = p *)
|
||||
(f : double -> double, (* function double to double *)
|
||||
pinit : double, (* initial estimate *)
|
||||
tol : double, (* tolerance *)
|
||||
maxiter : int) (* maximum number of iterations *)
|
||||
: Option (double) = (* return a double, IF tolerance is met *)
|
||||
let
|
||||
var p0 : double = pinit
|
||||
var p : double = aitken (f, p0)
|
||||
var iter : int = 1 (* iteration counter *)
|
||||
in
|
||||
while (abs (p - p0) > tol andalso iter < maxiter)
|
||||
begin
|
||||
p0 := p;
|
||||
p := aitken (f, p0);
|
||||
iter := iter + 1
|
||||
end;
|
||||
if abs (p - p0) > tol then None () else Some (p)
|
||||
end
|
||||
|
||||
The function <code>steffensen_aitken</code> will find a fixed point for us, but how can we use that to find a root? In particular, suppose one wants to find <math>t</math> such that <math>g(t) = 0</math>. Then what one can do (and what we will try to do) is find a fixed point <math>p</math> of <math>f(t) = g(t) + t</math>. In that case, <math>p = f(p) = g(p) + p</math>, and therefore <math>g(p) = 0</math>.
|
||||
|
||||
===The problem we wish to solve===
|
||||
|
||||
Suppose we have two quadratic planar [[wp:Bézier_curve|Bézier curves]], with control points <math>(-1,0), (0,10), (1,0)</math> and <math>(2,1), (-8,2), (2,3)</math>, respectively. These two parabolas are shown in the following figure. As you can see, they intersect at four points.
|
||||
|
||||
|
||||
::[[File:Steffensen's method Figure-1.png|none|alt=Two parabolas in the plane that intersect at four points, plus their Bézier control polygons.]]
|
||||
|
||||
|
||||
We want to try to find the points of intersection.
|
||||
|
||||
The method we will use is ''implicitization''. In this method, one first rewrites one of the curves as an [[wp:Implicit_function|implicit equation]] in <math>x</math> and <math>y</math>. For this we will use the parabola that is convex upwards: it has implicit equation <math>5x^2 + y - 5 = 0</math>. Then what one does is plug the parametric equations of the ''other'' curve into the implicit equation. The resulting equation is <math>5(x(t))^2 + y(t) - 5 = 0</math>, where <math>t</math> is the independent parameter for the curve that is convex ''leftwards''. After expansion, this will be a degree-4 equation in <math>t</math>. Find its four roots and you have found the points of intersection of the two curves.
|
||||
|
||||
That is easier said than done.
|
||||
|
||||
There are excellent ways to solve this problem, but we will not be using those. Our purpose, after all, is to illustrate Steffensen's method: both its advantages and its drawbacks. Let us look at an advantage: to use Steffensen's method (which requires only function values, not derivatives), we do not actually have to expand that polynomial in <math>t</math>. Instead, we can evaluate <math>x(t)</math> and <math>y(t)</math> and plug the resulting numbers into the implicit equation. What is more, we do not even need to write <math>x(t)</math> and <math>y(t)</math> as polynomials, but instead can evaluate them directly from their control points, using [[wp:De_Casteljau's_algorithm|de Casteljau's algorithm]]:
|
||||
|
||||
fun de_casteljau
|
||||
(c0 : double, (* control point coordinates (one axis) *)
|
||||
c1 : double,
|
||||
c2 : double,
|
||||
t : double) (* the independent parameter *)
|
||||
: double = (* value of x(t) or y(t) *)
|
||||
let
|
||||
val s = 1.0 - t
|
||||
val c01 = (s * c0) + (t * c1)
|
||||
val c12 = (s * c1) + (t * c2)
|
||||
val c012 = (s * c01) + (t * c12)
|
||||
in
|
||||
c012
|
||||
end
|
||||
|
||||
fun x_convex_left_parabola (t : double) : double =
|
||||
de_casteljau (2.0, ~8.0, 2.0, t)
|
||||
|
||||
fun y_convex_left_parabola (t : double) : double =
|
||||
de_casteljau (1.0, 2.0, 3.0, t)
|
||||
|
||||
Plugging <math>x(t)</math> and <math>y(t)</math> into the implicit equation, and writing <math>f(t)</math> as the function whose fixed points are to be found:
|
||||
|
||||
fun implicit_equation (x : double, y : double) : double =
|
||||
(5.0 * x * x) + y - 5.0
|
||||
|
||||
fun f (t : double) : double = (* find fixed points of this function *)
|
||||
let
|
||||
val x = x_convex_left_parabola (t)
|
||||
val y = y_convex_left_parabola (t)
|
||||
in
|
||||
implicit_equation (x, y) + t
|
||||
end
|
||||
|
||||
What is left to be done is simple, but tricky: use the <code>steffensen_aitken</code> function to find those fixed points. This is where a huge disadvantage of Steffensen's method (shared with Newton's method) will be illustrated. What is likely to happen is that only some of the intersection points will be found. Furthermore, for most of our initial estimates, Steffensen's method will not give us an answer at all.
|
||||
|
||||
===The task===
|
||||
|
||||
Use the methods described above to try to find intersection points of the two parabolas. For initial estimates, use <math>{\it pinit}=t_{0}=0.0,0.1,0.2,\cdots,0.9,1.0</math>. Choose reasonable settings for tolerance and maximum iterations. (The [[#ATS|ATS]] example has 0.00000001 and 1000, respectively.) For each initial estimate, if tolerance was not met before maximum iterations was reached, print that there was no answer. On the other hand, if there was an answer, plug the resulting value of <math>t</math> into <math>x(t)</math> and <math>y(t)</math> to find the intersection point <math>(x,y)</math>. Check that that this answer is correct, by plugging <math>(x,y)</math> into the implicit equation. If it is not correct, print that Steffensen's method gave a spurious answer. Otherwise print the value of <math>(x,y)</math>.
|
||||
|
||||
(''The [[#ATS|ATS]] example has purposely been written in a fashion to be readable as if it were pseudocode. You may use it as a reference implementation.'')
|
||||
|
||||
===See also===
|
||||
* [[Bézier_curves/Intersections|Bézier curves/Intersections]]
|
||||
|
||||
80
Task/Steffensens-method/ALGOL-68/steffensens-method.alg
Normal file
80
Task/Steffensens-method/ALGOL-68/steffensens-method.alg
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
BEGIN # Steffenses's method - translated from the ATS sample #
|
||||
|
||||
MODE OPTIONALREAL = UNION( VOID, REAL );
|
||||
|
||||
# Aitken's extrapolation #
|
||||
PROC aitken = ( PROC(REAL)REAL f # function double to double #
|
||||
, REAL p0 # initial fixed point estimate #
|
||||
) REAL:
|
||||
BEGIN
|
||||
REAL p1 = f( p0 );
|
||||
REAL p2 = f( p1 );
|
||||
REAL p1m0 = p1 - p0;
|
||||
p0 - ( p1m0 * p1m0 ) / ( p2 - ( 2 * p1 ) + p0 )
|
||||
END;
|
||||
|
||||
# finds fixed point p such that f(p) = p #
|
||||
PROC steffensen aitken = ( PROC(REAL)REAL f # function double to double #
|
||||
, REAL pinit # initial estimate #
|
||||
, REAL tol # tolerance #
|
||||
, INT maxiter # maximum number of iterations #
|
||||
) OPTIONALREAL: # return a REAL, IF tolerance is met #
|
||||
BEGIN
|
||||
REAL p0 := pinit;
|
||||
REAL p := aitken( f, p0 ); # first iteration #
|
||||
TO max iter - 1 WHILE ABS ( p - p0 ) > tol DO
|
||||
p0 := p;
|
||||
p := aitken( f, p0 )
|
||||
OD;
|
||||
IF ABS ( p - p0 ) > tol THEN EMPTY ELSE p FI
|
||||
END;
|
||||
|
||||
PROC de casteljau = ( REAL c0 # control point coordinates (one axis) #
|
||||
, REAL c1
|
||||
, REAL c2
|
||||
, REAL t # the independent parameter #
|
||||
) REAL: # value of x(t) or y(t) #
|
||||
BEGIN
|
||||
REAL s = 1 - t;
|
||||
REAL c01 = ( s * c0 ) + ( t * c1 );
|
||||
REAL c12 = ( s * c1 ) + ( t * c2 );
|
||||
( s * c01 ) + ( t * c12 )
|
||||
END;
|
||||
|
||||
PROC x convex left parabola = ( REAL t )REAL: de casteljau( 2, -8, 2, t );
|
||||
PROC y convex left parabola = ( REAL t )REAL: de casteljau( 1, 2, 3, t );
|
||||
|
||||
PROC implicit equation = ( REAL x, y )REAL: ( 5 * x * x ) + y - 5;
|
||||
|
||||
PROC f = ( REAL t )REAL: # find fixed points of this function #
|
||||
BEGIN
|
||||
REAL x = x convex left parabola( t );
|
||||
REAL y = y convex left parabola( t );
|
||||
implicit equation( x, y ) + t
|
||||
END;
|
||||
|
||||
# returns v formatted with 1 digit before thw point and 6 after, with a leading sign only if negative #
|
||||
OP FMT = ( REAL v )STRING: fixed( v, IF v < 0 THEN -9 ELSE -8 FI, 6 );
|
||||
|
||||
BEGIN
|
||||
REAL t0 := 0;
|
||||
TO 11 DO
|
||||
print( ( "t0 = ", FMT t0, " : " ) );
|
||||
CASE steffensen aitken( f, t0, 0.00000001, 1000 )
|
||||
IN ( VOID ): print( ( "no answer", newline) )
|
||||
, ( REAL t ):
|
||||
IF REAL x = x convex left parabola( t )
|
||||
, y = y convex left parabola( t )
|
||||
;
|
||||
ABS implicit equation( x, y ) <= 0.000001
|
||||
THEN
|
||||
print( ( "intersection at (", FMT x, ", ", FMT y, ")", newline ) )
|
||||
ELSE
|
||||
print( ( "spurious solution", newline ) )
|
||||
FI
|
||||
ESAC;
|
||||
t0 +:= 0.1
|
||||
OD
|
||||
END
|
||||
|
||||
END
|
||||
97
Task/Steffensens-method/ATS/steffensens-method.ats
Normal file
97
Task/Steffensens-method/ATS/steffensens-method.ats
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
fun aitken (* Aitken's extrapolation *)
|
||||
(f : double -> double, (* function double to double *)
|
||||
p0 : double) (* initial fixed point estimate *)
|
||||
: double =
|
||||
let
|
||||
val p1 = f(p0)
|
||||
val p2 = f(p1)
|
||||
val p1m0 = p1 - p0
|
||||
in
|
||||
p0 - (p1m0 * p1m0) / (p2 - (2.0 * p1) + p0)
|
||||
end
|
||||
|
||||
fun steffensen_aitken (* finds fixed point p such that f(p) = p *)
|
||||
(f : double -> double, (* function double to double *)
|
||||
pinit : double, (* initial estimate *)
|
||||
tol : double, (* tolerance *)
|
||||
maxiter : int) (* maximum number of iterations *)
|
||||
: Option (double) = (* return a double, IF tolerance is met *)
|
||||
let
|
||||
var p0 : double = pinit
|
||||
var p : double = aitken (f, p0)
|
||||
var iter : int = 1 (* iteration counter *)
|
||||
in
|
||||
while (abs (p - p0) > tol andalso iter < maxiter)
|
||||
begin
|
||||
p0 := p;
|
||||
p := aitken (f, p0);
|
||||
iter := iter + 1
|
||||
end;
|
||||
if abs (p - p0) > tol then None () else Some (p)
|
||||
end
|
||||
|
||||
fun de_casteljau
|
||||
(c0 : double, (* control point coordinates (one axis) *)
|
||||
c1 : double,
|
||||
c2 : double,
|
||||
t : double) (* the independent parameter *)
|
||||
: double = (* value of x(t) or y(t) *)
|
||||
let
|
||||
val s = 1.0 - t
|
||||
val c01 = (s * c0) + (t * c1)
|
||||
val c12 = (s * c1) + (t * c2)
|
||||
val c012 = (s * c01) + (t * c12)
|
||||
in
|
||||
c012
|
||||
end
|
||||
|
||||
fun x_convex_left_parabola (t : double) : double =
|
||||
de_casteljau (2.0, ~8.0, 2.0, t)
|
||||
|
||||
fun y_convex_left_parabola (t : double) : double =
|
||||
de_casteljau (1.0, 2.0, 3.0, t)
|
||||
|
||||
fun implicit_equation (x : double, y : double) : double =
|
||||
(5.0 * x * x) + y - 5.0
|
||||
|
||||
fun f (t : double) : double = (* find fixed points of this function *)
|
||||
let
|
||||
val x = x_convex_left_parabola (t)
|
||||
val y = y_convex_left_parabola (t)
|
||||
in
|
||||
implicit_equation (x, y) + t
|
||||
end
|
||||
|
||||
implement main0 () =
|
||||
let
|
||||
var i : int = 0
|
||||
var t0 : double = 0.0
|
||||
in
|
||||
while (not (i = 11))
|
||||
begin
|
||||
begin
|
||||
print! ("t0 = ", t0, " : ");
|
||||
case steffensen_aitken (f, t0, 0.00000001, 1000) of
|
||||
| None () => println! ("no answer")
|
||||
| Some (t) =>
|
||||
let
|
||||
val x = x_convex_left_parabola (t)
|
||||
val y = y_convex_left_parabola (t)
|
||||
in
|
||||
if abs (implicit_equation (x, y)) <= 0.000001 then
|
||||
println! ("intersection at (", x, ", ", y, ")")
|
||||
else
|
||||
(* In my experience, it is possible for the algorithm
|
||||
to achieve tolerance and yet give a spurious
|
||||
answer. Exploration of this phenomenon is beyond
|
||||
the scope of this task. Such spurious answers are
|
||||
easy to discard. *)
|
||||
println! ("spurious solution")
|
||||
end
|
||||
end;
|
||||
i := i + 1;
|
||||
t0 := t0 + 0.1
|
||||
end
|
||||
end
|
||||
72
Task/Steffensens-method/C++/steffensens-method.cpp
Normal file
72
Task/Steffensens-method/C++/steffensens-method.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
double aitken(double (*f)(double), double p0) {
|
||||
double p1 = f(p0);
|
||||
double p2 = f(p1);
|
||||
double p1m0 = p1 - p0;
|
||||
return p0 - p1m0 * p1m0 / (p2 - 2.0 * p1 + p0);
|
||||
}
|
||||
|
||||
double steffensenAitken(double (*f)(double), double pinit, double tol, int maxiter) {
|
||||
double p0 = pinit;
|
||||
double p = aitken(f, p0);
|
||||
int iter = 1;
|
||||
while (fabs(p - p0) > tol && iter < maxiter) {
|
||||
p0 = p;
|
||||
p = aitken(f, p0);
|
||||
++iter;
|
||||
}
|
||||
if (fabs(p - p0) > tol) return nan("");
|
||||
return p;
|
||||
}
|
||||
|
||||
double deCasteljau(double c0, double c1, double c2, double t) {
|
||||
double s = 1.0 - t;
|
||||
double c01 = s * c0 + t * c1;
|
||||
double c12 = s * c1 + t * c2;
|
||||
return s * c01 + t * c12;
|
||||
}
|
||||
|
||||
double xConvexLeftParabola(double t) {
|
||||
return deCasteljau(2.0, -8.0, 2.0, t);
|
||||
}
|
||||
|
||||
double yConvexRightParabola(double t) {
|
||||
return deCasteljau(1.0, 2.0, 3.0, t);
|
||||
}
|
||||
|
||||
double implicitEquation(double x, double y) {
|
||||
return 5.0 * x * x + y - 5.0;
|
||||
}
|
||||
|
||||
double f(double t) {
|
||||
double x = xConvexLeftParabola(t);
|
||||
double y = yConvexRightParabola(t);
|
||||
return implicitEquation(x, y) + t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double t0 = 0.0, t, x, y;
|
||||
int i;
|
||||
for (i = 0; i < 11; ++i) {
|
||||
cout << "t0 = " << setprecision(1) << t0 << " : ";
|
||||
t = steffensenAitken(f, t0, 0.00000001, 1000);
|
||||
if (isnan(t)) {
|
||||
cout << "no answer << endl;
|
||||
} else {
|
||||
x = xConvexLeftParabola(t);
|
||||
y = yConvexRightParabola(t);
|
||||
if (fabs(implicitEquation(x, y)) <= 0.000001) {
|
||||
cout << "intersection at (" << x << ", " << y << ")" << endl;
|
||||
} else {
|
||||
cout << "spurious solution" << endl;
|
||||
}
|
||||
}
|
||||
t0 += 0.1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
69
Task/Steffensens-method/C/steffensens-method.c
Normal file
69
Task/Steffensens-method/C/steffensens-method.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double aitken(double (*f)(double), double p0) {
|
||||
double p1 = f(p0);
|
||||
double p2 = f(p1);
|
||||
double p1m0 = p1 - p0;
|
||||
return p0 - p1m0 * p1m0 / (p2 - 2.0 * p1 + p0);
|
||||
}
|
||||
|
||||
double steffensenAitken(double (*f)(double), double pinit, double tol, int maxiter) {
|
||||
double p0 = pinit;
|
||||
double p = aitken(f, p0);
|
||||
int iter = 1;
|
||||
while (fabs(p - p0) > tol && iter < maxiter) {
|
||||
p0 = p;
|
||||
p = aitken(f, p0);
|
||||
++iter;
|
||||
}
|
||||
if (fabs(p - p0) > tol) return nan("");
|
||||
return p;
|
||||
}
|
||||
|
||||
double deCasteljau(double c0, double c1, double c2, double t) {
|
||||
double s = 1.0 - t;
|
||||
double c01 = s * c0 + t * c1;
|
||||
double c12 = s * c1 + t * c2;
|
||||
return s * c01 + t * c12;
|
||||
}
|
||||
|
||||
double xConvexLeftParabola(double t) {
|
||||
return deCasteljau(2.0, -8.0, 2.0, t);
|
||||
}
|
||||
|
||||
double yConvexRightParabola(double t) {
|
||||
return deCasteljau(1.0, 2.0, 3.0, t);
|
||||
}
|
||||
|
||||
double implicitEquation(double x, double y) {
|
||||
return 5.0 * x * x + y - 5.0;
|
||||
}
|
||||
|
||||
double f(double t) {
|
||||
double x = xConvexLeftParabola(t);
|
||||
double y = yConvexRightParabola(t);
|
||||
return implicitEquation(x, y) + t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double t0 = 0.0, t, x, y;
|
||||
int i;
|
||||
for (i = 0; i < 11; ++i) {
|
||||
printf("t0 = %0.1f : ", t0);
|
||||
t = steffensenAitken(f, t0, 0.00000001, 1000);
|
||||
if (isnan(t)) {
|
||||
printf("no answer\n");
|
||||
} else {
|
||||
x = xConvexLeftParabola(t);
|
||||
y = yConvexRightParabola(t);
|
||||
if (fabs(implicitEquation(x, y)) <= 0.000001) {
|
||||
printf("intersection at (%f, %f)\n", x, y);
|
||||
} else {
|
||||
printf("spurious solution\n");
|
||||
}
|
||||
}
|
||||
t0 += 0.1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
57
Task/Steffensens-method/EasyLang/steffensens-method.easy
Normal file
57
Task/Steffensens-method/EasyLang/steffensens-method.easy
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
proc deCasteljau c0 c1 c2 t . r .
|
||||
s = 1 - t
|
||||
c01 = s * c0 + t * c1
|
||||
c12 = s * c1 + t * c2
|
||||
r = s * c01 + t * c12
|
||||
.
|
||||
proc xConvexLeftPar t . r .
|
||||
call deCasteljau 2 (-8) 2 t r
|
||||
.
|
||||
proc yConvexRightPar t . r .
|
||||
call deCasteljau 1 2 3 t r
|
||||
.
|
||||
proc implicitEq x y . r .
|
||||
r = 5 * x * x + y - 5
|
||||
.
|
||||
proc f t . r .
|
||||
call xConvexLeftPar t x
|
||||
call yConvexRightPar t y
|
||||
call implicitEq x y r
|
||||
r += t
|
||||
.
|
||||
proc aitken p0 . r .
|
||||
call f p0 p1
|
||||
call f p1 p2
|
||||
p1m0 = p1 - p0
|
||||
r = p0 - p1m0 * p1m0 / (p2 - 2 * p1 + p0)
|
||||
.
|
||||
proc steffAitken p0 tol maxiter . p .
|
||||
for i to maxiter
|
||||
call aitken p0 p
|
||||
if abs (p - p0) < tol
|
||||
break 2
|
||||
.
|
||||
p0 = p
|
||||
.
|
||||
p = number "nan"
|
||||
.
|
||||
for i to 11
|
||||
numfmt 1 0
|
||||
write "t0 = " & t0 & " : "
|
||||
call steffAitken t0 0.00000001 1000 t
|
||||
numfmt 3 0
|
||||
if t <> t
|
||||
# nan
|
||||
print "no answer"
|
||||
else
|
||||
call xConvexLeftPar t x
|
||||
call yConvexRightPar t y
|
||||
call implicitEq x y r
|
||||
if abs r <= 0.000001
|
||||
print "intersection at (" & x & " " & y & ")"
|
||||
else
|
||||
print "spurious solution"
|
||||
.
|
||||
.
|
||||
t0 += 0.1
|
||||
.
|
||||
69
Task/Steffensens-method/FreeBASIC/steffensens-method.basic
Normal file
69
Task/Steffensens-method/FreeBASIC/steffensens-method.basic
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
Function aitken(f As Function(As Double) As Double, p0 As Double) As Double
|
||||
Dim As Double p1 = f(p0)
|
||||
Dim As Double p2 = f(p1)
|
||||
Dim As Double p1m0 = p1 - p0
|
||||
|
||||
Return p0 - (p1m0 * p1m0) / (p2 - (2 * p1) + p0)
|
||||
End Function
|
||||
|
||||
Function steffensenAitken(f As Function(As Double) As Double, pinit As Double, tol As Double, maxiter As Integer) As Double
|
||||
Dim As Double p0 = pinit
|
||||
Dim As Double p = aitken(f, p0)
|
||||
Dim As Integer iter = 1
|
||||
While Abs(p-p0) > tol Andalso iter < maxiter
|
||||
p0 = p
|
||||
p = aitken (f, p0)
|
||||
iter += 1
|
||||
Wend
|
||||
|
||||
If Abs (p - p0) > tol Then Return 0 Else Return p
|
||||
End Function
|
||||
|
||||
Function deCasteljau(c0 As Double, c1 As Double, c2 As Double, t As Double) As Double
|
||||
Dim As Double s = 1 - t
|
||||
Dim As Double c01 = (s * c0) + (t * c1)
|
||||
Dim As Double c12 = (s * c1) + (t * c2)
|
||||
Dim As Double c012 = (s * c01) + (t * c12)
|
||||
Return c012
|
||||
End Function
|
||||
|
||||
Function xConvexLeftParabola(t As Double) As Double
|
||||
Return deCasteljau(2, -8, 2, t)
|
||||
End Function
|
||||
|
||||
Function yConvexLeftParabola(t As Double) As Double
|
||||
Return deCasteljau(1, 2, 3, t)
|
||||
End Function
|
||||
|
||||
Function implicitEquation(x As Double, y As Double) As Double
|
||||
Return (5 * x * x) + y - 5
|
||||
End Function
|
||||
|
||||
Function f(t As Double) As Double
|
||||
Dim As Double x = xConvexLeftParabola(t)
|
||||
Dim As Double y = yConvexLeftParabola(t)
|
||||
|
||||
Return implicitEquation(x, y) + t
|
||||
End Function
|
||||
|
||||
Dim As Double t0 = 0
|
||||
|
||||
For i As Integer = 0 To 10
|
||||
Print Using "t0 = #.# : "; t0;
|
||||
Dim As Double t = steffensenAitken(@f, t0, 0.00000001, 1000)
|
||||
If t = 0 Then
|
||||
Print "no answer"
|
||||
Else
|
||||
Dim As Double x = xConvexLeftParabola(t)
|
||||
Dim As Double y = yConvexLeftParabola(t)
|
||||
|
||||
If Abs(implicitEquation(x, y)) <= 0.000001 Then
|
||||
Print Using "intersection at (##.######, ##.######)"; x; y
|
||||
Else
|
||||
Print "spurious solution"
|
||||
End If
|
||||
End If
|
||||
t0 += 0.1
|
||||
Next
|
||||
|
||||
Sleep
|
||||
75
Task/Steffensens-method/Go/steffensens-method.go
Normal file
75
Task/Steffensens-method/Go/steffensens-method.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type fun = func(float64) float64
|
||||
|
||||
func aitken(f fun, p0 float64) float64 {
|
||||
p1 := f(p0)
|
||||
p2 := f(p1)
|
||||
p1m0 := p1 - p0
|
||||
return p0 - p1m0*p1m0/(p2-2.0*p1+p0)
|
||||
}
|
||||
|
||||
func steffensenAitken(f fun, pinit, tol float64, maxiter int) float64 {
|
||||
p0 := pinit
|
||||
p := aitken(f, p0)
|
||||
iter := 1
|
||||
for math.Abs(p-p0) > tol && iter < maxiter {
|
||||
p0 = p
|
||||
p = aitken(f, p0)
|
||||
iter++
|
||||
}
|
||||
if math.Abs(p-p0) > tol {
|
||||
return math.NaN()
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func deCasteljau(c0, c1, c2, t float64) float64 {
|
||||
s := 1.0 - t
|
||||
c01 := s*c0 + t*c1
|
||||
c12 := s*c1 + t*c2
|
||||
return s*c01 + t*c12
|
||||
}
|
||||
|
||||
func xConvexLeftParabola(t float64) float64 {
|
||||
return deCasteljau(2.0, -8.0, 2.0, t)
|
||||
}
|
||||
|
||||
func yConvexRightParabola(t float64) float64 {
|
||||
return deCasteljau(1.0, 2.0, 3.0, t)
|
||||
}
|
||||
|
||||
func implicitEquation(x, y float64) float64 {
|
||||
return 5.0*x*x + y - 5.0
|
||||
}
|
||||
|
||||
func f(t float64) float64 {
|
||||
x := xConvexLeftParabola(t)
|
||||
y := yConvexRightParabola(t)
|
||||
return implicitEquation(x, y) + t
|
||||
}
|
||||
|
||||
func main() {
|
||||
t0 := 0.0
|
||||
for i := 0; i < 11; i++ {
|
||||
fmt.Printf("t0 = %0.1f : ", t0)
|
||||
t := steffensenAitken(f, t0, 0.00000001, 1000)
|
||||
if math.IsNaN(t) {
|
||||
fmt.Println("no answer")
|
||||
} else {
|
||||
x := xConvexLeftParabola(t)
|
||||
y := yConvexRightParabola(t)
|
||||
if math.Abs(implicitEquation(x, y)) <= 0.000001 {
|
||||
fmt.Printf("intersection at (%f, %f)\n", x, y)
|
||||
} else {
|
||||
fmt.Println("spurious solution")
|
||||
}
|
||||
}
|
||||
t0 += 0.1
|
||||
}
|
||||
}
|
||||
68
Task/Steffensens-method/Java/steffensens-method.java
Normal file
68
Task/Steffensens-method/Java/steffensens-method.java
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import java.util.Optional;
|
||||
|
||||
public class Steffensen {
|
||||
static double aitken(double p0) {
|
||||
double p1 = f(p0);
|
||||
double p2 = f(p1);
|
||||
double p1m0 = p1 - p0;
|
||||
return p0 - p1m0 * p1m0 / (p2 - 2.0 * p1 + p0);
|
||||
}
|
||||
|
||||
static Optional<Double> steffensenAitken(double pinit, double tol, int maxiter) {
|
||||
double p0 = pinit;
|
||||
double p = aitken(p0);
|
||||
int iter = 1;
|
||||
while (Math.abs(p - p0) > tol && iter < maxiter) {
|
||||
p0 = p;
|
||||
p = aitken(p0);
|
||||
iter++;
|
||||
}
|
||||
if (Math.abs(p - p0) > tol) return Optional.empty();
|
||||
return Optional.of(p);
|
||||
}
|
||||
|
||||
static double deCasteljau(double c0, double c1, double c2, double t) {
|
||||
double s = 1.0 - t;
|
||||
double c01 = s * c0 + t * c1;
|
||||
double c12 = s * c1 + t * c2;
|
||||
return s * c01 + t * c12;
|
||||
}
|
||||
|
||||
static double xConvexLeftParabola(double t) {
|
||||
return deCasteljau(2.0, -8.0, 2.0, t);
|
||||
}
|
||||
|
||||
static double yConvexRightParabola(double t) {
|
||||
return deCasteljau(1.0, 2.0, 3.0, t);
|
||||
}
|
||||
|
||||
static double implicitEquation(double x, double y) {
|
||||
return 5.0 * x * x + y - 5.0;
|
||||
}
|
||||
|
||||
static double f(double t) {
|
||||
double x = xConvexLeftParabola(t);
|
||||
double y = yConvexRightParabola(t);
|
||||
return implicitEquation(x, y) + t;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
double t0 = 0.0;
|
||||
for (int i = 0; i < 11; ++i) {
|
||||
System.out.printf("t0 = %3.1f : ", t0);
|
||||
Optional<Double> t = steffensenAitken(t0, 0.00000001, 1000);
|
||||
if (!t.isPresent()) {
|
||||
System.out.println("no answer");
|
||||
} else {
|
||||
double x = xConvexLeftParabola(t.get());
|
||||
double y = yConvexRightParabola(t.get());
|
||||
if (Math.abs(implicitEquation(x, y)) <= 0.000001) {
|
||||
System.out.printf("intersection at (%f, %f)\n", x, y);
|
||||
} else {
|
||||
System.out.println("spurious solution");
|
||||
}
|
||||
}
|
||||
t0 += 0.1;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Task/Steffensens-method/Nim/steffensens-method.nim
Normal file
53
Task/Steffensens-method/Nim/steffensens-method.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import std/[math, strformat]
|
||||
|
||||
proc aitken(f: proc(x: float): float; p0: float): float =
|
||||
let p1 = f(p0)
|
||||
let p2 = f(p1)
|
||||
let p1m0 = p1 - p0
|
||||
result = p0 - p1m0 * p1m0 / (p2 - 2 * p1 + p0)
|
||||
|
||||
proc steffensenAitken(f: proc(x: float): float; pinit, tol: float; maxiter: int): float =
|
||||
var p0 = pinit
|
||||
result = aitken(f, p0)
|
||||
var iter = 1
|
||||
while abs(result - p0) > tol and iter < maxiter:
|
||||
p0 = result
|
||||
result = aitken(f, p0)
|
||||
inc iter
|
||||
if abs(result - p0) > tol: return Nan
|
||||
|
||||
func deCasteljau(c0, c1, c2, t: float): float =
|
||||
let s = 1 - t
|
||||
let c01 = s * c0 + t * c1
|
||||
let c12 = s * c1 + t * c2
|
||||
result = s * c01 + t * c12
|
||||
|
||||
template xConvexLeftParabola(t: float): float =
|
||||
deCasteljau(2, -8, 2, t)
|
||||
|
||||
template yConvexRightParabola(t: float): float =
|
||||
deCasteljau(1, 2, 3, t)
|
||||
|
||||
func implicitEquation(x, y: float): float =
|
||||
5 * x * x + y - 5
|
||||
|
||||
func f(t: float): float =
|
||||
let x = xConvexLeftParabola(t)
|
||||
let y = yConvexRightParabola(t)
|
||||
result = implicitEquation(x, y) + t
|
||||
|
||||
var t0 = 0.0
|
||||
var x, y: float
|
||||
for i in 0..10:
|
||||
stdout.write &"t0 = {t0:0.1f} → "
|
||||
let t = steffensenAitken(f, t0, 0.00000001, 1000)
|
||||
if t.isNan:
|
||||
echo "no answer"
|
||||
else:
|
||||
x = xConvexLeftParabola(t);
|
||||
y = yConvexRightParabola(t);
|
||||
if abs(implicitEquation(x, y)) <= 0.000001:
|
||||
echo &"intersection at ({x:.6f}, {y:.6f})"
|
||||
else:
|
||||
echo "spurious solution"
|
||||
t0 += 0.1
|
||||
62
Task/Steffensens-method/Phix/steffensens-method.phix
Normal file
62
Task/Steffensens-method/Phix/steffensens-method.phix
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">aitken</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">p0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">p1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p0</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">p2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">p1m0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">p0</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">-</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">p1m0</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">p1m0</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">p2</span> <span style="color: #0000FF;">-</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">p0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">steffensenAitken</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">maxiter</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">pinit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tol</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pinit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">aitken</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">iter</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">-</span><span style="color: #000000;">p0</span><span style="color: #0000FF;">)></span><span style="color: #000000;">tol</span> <span style="color: #008080;">and</span> <span style="color: #000000;">iter</span><span style="color: #0000FF;"><</span><span style="color: #000000;">maxiter</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">p0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
|
||||
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">aitken</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">p0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">iter</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">-</span><span style="color: #000000;">p0</span><span style="color: #0000FF;">)></span><span style="color: #000000;">tol</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"none"</span><span style="color: #0000FF;">:</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">deCasteljau</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">c0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">c01</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c0</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">c12</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c2</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">c012</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c01</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">c12</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">c012</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">xConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">deCasteljau</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">yConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">deCasteljau</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">implicitEquation</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">5</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">5</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">implicitEquation</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">t</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"t0 = %.1f : "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">steffensenAitken</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.00000001</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"no answer\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yConvexLeftParabola</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">implicitEquation</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">0.000001</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"intersection at (%.6f, %.6f)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"spurious solution\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
56
Task/Steffensens-method/Python/steffensens-method.py
Normal file
56
Task/Steffensens-method/Python/steffensens-method.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
from math import nan, isnan
|
||||
from numpy import arange
|
||||
|
||||
|
||||
def aitken(f, p0):
|
||||
""" Aitken's extrapolation """
|
||||
p1 = f(p0)
|
||||
p2 = f(p1)
|
||||
return p0 - (p1 - p0)**2 / (p2 - 2 * p1 + p0)
|
||||
|
||||
def steffensen_aitken(f, pinit, tol, maxiter):
|
||||
""" Steffensen's method using Aitken """
|
||||
p0 = pinit
|
||||
p = aitken(f, p0)
|
||||
iter = 1
|
||||
while abs(p - p0) > tol and iter < maxiter:
|
||||
p0 = p
|
||||
p = aitken(f, p0)
|
||||
iter += 1
|
||||
return nan if abs(p - p0) > tol else p
|
||||
|
||||
def deCasteljau(c0, c1, c2, t):
|
||||
""" deCasteljau function """
|
||||
s = 1.0 - t
|
||||
return s * (s * c0 + t * c1) + t * (s * c1 + t * c2)
|
||||
|
||||
def xConvexLeftParabola(t): return deCasteljau(2, -8, 2, t)
|
||||
def yConvexRightParabola(t): return deCasteljau(1, 2, 3, t)
|
||||
def implicit_equation(x, y): return 5 * x**2 + y - 5
|
||||
|
||||
def f(t):
|
||||
""" Outside of NumPy arithmetic may return NoneType on overflow """
|
||||
if type(t) == type(None):
|
||||
return nan
|
||||
return implicit_equation(xConvexLeftParabola(t), yConvexRightParabola(t)) + t
|
||||
|
||||
def test_steffensen(tol=0.00000001, iters=1000, stepsize=0.1):
|
||||
""" test the example """
|
||||
for t0 in arange(0, 1.1, stepsize):
|
||||
print(f't0 = {t0:0.1f} : ', end='')
|
||||
t = steffensen_aitken(f, t0, tol, iters)
|
||||
if isnan(t):
|
||||
print('no answer')
|
||||
else:
|
||||
x = xConvexLeftParabola(t)
|
||||
y = yConvexRightParabola(t)
|
||||
if abs(implicit_equation(x, y)) <= tol:
|
||||
print(f'intersection at ({x}, {y})')
|
||||
else:
|
||||
print('spurious solution')
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
test_steffensen()
|
||||
106
Task/Steffensens-method/RATFOR/steffensens-method.ratfor
Normal file
106
Task/Steffensens-method/RATFOR/steffensens-method.ratfor
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
function aitken (f, p0)
|
||||
implicit none
|
||||
|
||||
double precision f, p0, aitken
|
||||
external f
|
||||
|
||||
double precision p1, p2
|
||||
|
||||
p1 = f(p0)
|
||||
p2 = f(p1)
|
||||
aitken = p0 - (p1 - p0)**2 / (p2 - (2.0d0 * p1) + p0)
|
||||
end
|
||||
|
||||
subroutine steff (f, pinit, tol, mxiter, ierr, pfinal)
|
||||
implicit none
|
||||
|
||||
double precision f, pinit, tol, pfinal
|
||||
integer mxiter, ierr
|
||||
external f
|
||||
|
||||
double precision p0, p, aitken
|
||||
integer iter
|
||||
external aitken
|
||||
|
||||
p0 = pinit
|
||||
p = aitken (f, p0)
|
||||
iter = 1
|
||||
while (abs (p - p0) > tol && iter < mxiter)
|
||||
{
|
||||
p0 = p
|
||||
p = aitken (f, p0)
|
||||
iter = iter + 1
|
||||
}
|
||||
if (abs (p - p0) > tol)
|
||||
ierr = -1
|
||||
else
|
||||
{
|
||||
ierr = 0
|
||||
pfinal = p
|
||||
}
|
||||
end
|
||||
|
||||
function dcstlj (c0, c1, c2, t)
|
||||
implicit none
|
||||
|
||||
double precision c0, c1, c2, t, dcstlj
|
||||
|
||||
double precision s, c01, c12, c012
|
||||
|
||||
s = 1.0d0 - t
|
||||
c01 = (s * c0) + (t * c1)
|
||||
c12 = (s * c1) + (t * c2)
|
||||
c012 = (s * c01) + (t * c12)
|
||||
dcstlj = c012
|
||||
end
|
||||
|
||||
function x (t)
|
||||
implicit none
|
||||
double precision x, t, dcstlj
|
||||
external dcstlj
|
||||
x = dcstlj (2.0d0, -8.0d0, 2.0d0, t)
|
||||
end
|
||||
|
||||
function y (t)
|
||||
implicit none
|
||||
double precision y, t, dcstlj
|
||||
external dcstlj
|
||||
y = dcstlj (1.0d0, 2.0d0, 3.0d0, t)
|
||||
end
|
||||
|
||||
function impleq (x, y)
|
||||
implicit none
|
||||
double precision x, y, impleq
|
||||
impleq = (5.0d0 * x * x) + y - 5.0d0
|
||||
end
|
||||
|
||||
function f (t)
|
||||
implicit none
|
||||
double precision f, t, x, y, impleq
|
||||
external x, y, impleq
|
||||
f = impleq (x(t), y(t)) + t
|
||||
end
|
||||
|
||||
program RCstef
|
||||
implicit none
|
||||
|
||||
double precision x, y, impleq, f
|
||||
external x, y, impleq, f, steff
|
||||
|
||||
double precision t0, t
|
||||
integer i, ierr
|
||||
|
||||
t0 = 0.0d0
|
||||
for (i = 0; i != 11; i = i + 1)
|
||||
{
|
||||
call steff (f, t0, 0.00000001d0, 1000, ierr, t)
|
||||
if (ierr < 0)
|
||||
write (*,*) "t0 = ", t0, " : no answer"
|
||||
else if (abs (impleq (x(t), y(t))) <= 0.000001)
|
||||
write (*,*) "t0 = ", t0, " : intersection at (", _
|
||||
x(t), ", ", y(t), ")"
|
||||
else
|
||||
write (*,*) "t0 = ", t0, " : spurious solution"
|
||||
t0 = t0 + 0.1d0
|
||||
}
|
||||
end
|
||||
57
Task/Steffensens-method/Wren/steffensens-method.wren
Normal file
57
Task/Steffensens-method/Wren/steffensens-method.wren
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import "./fmt" for Fmt
|
||||
|
||||
var aitken = Fn.new { |f, p0|
|
||||
var p1 = f.call(p0)
|
||||
var p2 = f.call(p1)
|
||||
var p1m0 = p1 - p0
|
||||
return p0 - p1m0 * p1m0 / (p2 - 2 * p1 + p0)
|
||||
}
|
||||
|
||||
var steffensenAitken = Fn.new { |f, pinit, tol, maxiter|
|
||||
var p0 = pinit
|
||||
var p = aitken.call(f, p0)
|
||||
var iter = 1
|
||||
while ((p - p0).abs > tol && iter < maxiter) {
|
||||
p0 = p
|
||||
p = aitken.call(f, p0)
|
||||
iter = iter + 1
|
||||
}
|
||||
if ((p - p0).abs > tol) return null
|
||||
return p
|
||||
}
|
||||
|
||||
var deCasteljau = Fn.new { |c0, c1, c2, t|
|
||||
var s = 1 - t
|
||||
var c01 = s * c0 + t * c1
|
||||
var c12 = s * c1 + t * c2
|
||||
return s * c01 + t * c12
|
||||
}
|
||||
|
||||
var xConvexLeftParabola = Fn.new { |t| deCasteljau.call(2, -8, 2, t) }
|
||||
var yConvexRightParabola = Fn.new { |t| deCasteljau.call(1, 2, 3, t) }
|
||||
|
||||
var implicitEquation = Fn.new { |x, y| 5 * x * x + y - 5 }
|
||||
|
||||
var f = Fn.new { |t|
|
||||
var x = xConvexLeftParabola.call(t)
|
||||
var y = yConvexRightParabola.call(t)
|
||||
return implicitEquation.call(x, y) + t
|
||||
}
|
||||
|
||||
var t0 = 0
|
||||
for (i in 0..10) {
|
||||
Fmt.write("t0 = $0.1f : ", t0)
|
||||
var t = steffensenAitken.call(f, t0, 0.00000001, 1000)
|
||||
if (!t) {
|
||||
Fmt.print("no answer")
|
||||
} else {
|
||||
var x = xConvexLeftParabola.call(t)
|
||||
var y = yConvexRightParabola.call(t)
|
||||
if (implicitEquation.call(x, y).abs <= 0.000001) {
|
||||
Fmt.print("intersection at ($f, $f)", x, y)
|
||||
} else {
|
||||
Fmt.print("spurious solution")
|
||||
}
|
||||
}
|
||||
t0 = t0 + 0.1
|
||||
}
|
||||
74
Task/Steffensens-method/XPL0/steffensens-method.xpl0
Normal file
74
Task/Steffensens-method/XPL0/steffensens-method.xpl0
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
include xpllib; \for Print
|
||||
def NaN = 1e308;
|
||||
|
||||
func real DeCasteljau(C0, C1, C2, T);
|
||||
real C0, C1, C2, T;
|
||||
real S, C01, C12;
|
||||
[S:= 1.0 - T;
|
||||
C01:= S*C0 + T*C1;
|
||||
C12:= S*C1 + T*C2;
|
||||
return S*C01 + T*C12;
|
||||
];
|
||||
|
||||
func real XConvexLeftParabola(T);
|
||||
real T;
|
||||
return DeCasteljau(2.0, -8.0, 2.0, T);
|
||||
|
||||
func real YConvexRightParabola(T);
|
||||
real T;
|
||||
return DeCasteljau(1.0, 2.0, 3.0, T);
|
||||
|
||||
func real ImplicitEquation(X, Y);
|
||||
real X, Y;
|
||||
return 5.0*X*X + Y - 5.0;
|
||||
|
||||
func real F(T);
|
||||
real T;
|
||||
real X, Y;
|
||||
[X:= XConvexLeftParabola(T);
|
||||
Y:= YConvexRightParabola(T);
|
||||
return ImplicitEquation(X, Y) + T;
|
||||
];
|
||||
|
||||
func real Aitken(P0);
|
||||
real P0;
|
||||
real P1, P2, P1M0;
|
||||
[P1:= F(P0);
|
||||
P2:= F(P1);
|
||||
P1M0:= P1 - P0;
|
||||
return P0 - P1M0 * P1M0 / (P2 - 2.0*P1 + P0);
|
||||
];
|
||||
|
||||
func real SteffensenAitken(PInit, Tol, MaxIter);
|
||||
real PInit, Tol; int MaxIter;
|
||||
real P0, P;
|
||||
int Iter;
|
||||
[P0:= PInit;
|
||||
P:= Aitken(P0);
|
||||
Iter:= 1;
|
||||
while abs(P-P0) > Tol and Iter < MaxIter do
|
||||
[P0:= P;
|
||||
P:= Aitken(P0);
|
||||
Iter:= Iter+1;
|
||||
];
|
||||
if abs(P-P0) > Tol then return NaN;
|
||||
return P;
|
||||
];
|
||||
|
||||
real T0, T, X, Y;
|
||||
int I;
|
||||
[T0:= 0.0;
|
||||
for I:= 0 to 10 do
|
||||
[Print("T0:= %1.1f : ", T0);
|
||||
T:= SteffensenAitken(T0, 0.00000001, 1000);
|
||||
if T = NaN then
|
||||
Print("No answer\n")
|
||||
else [X:= XConvexLeftParabola(T);
|
||||
Y:= YConvexRightParabola(T);
|
||||
if abs(ImplicitEquation(X, Y)) <= 0.000001 then
|
||||
Print("Intersection at (%1.6f, %1.6f)\n", X, Y)
|
||||
else Print("Spurious solution\n");
|
||||
];
|
||||
T0:= T0 + 0.1;
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue