Data update
This commit is contained in:
parent
796d366b97
commit
35bcdeebf8
504 changed files with 7045 additions and 610 deletions
57
Task/Steffensens-method/Julia/steffensens-method.julia
Normal file
57
Task/Steffensens-method/Julia/steffensens-method.julia
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
""" Aitken's extrapolation """
|
||||
function aitken(f, p0)
|
||||
p1 = f(p0)
|
||||
p2 = f(p1)
|
||||
return p0 - (p1 - p0)^2 / (p2 - 2 * p1 + p0)
|
||||
end
|
||||
|
||||
""" Steffensen's method using Aitken """
|
||||
function steffensen_aitken(f, pinit, tol, maxiter)
|
||||
p0 = pinit
|
||||
p = aitken(f, p0)
|
||||
iter = 1
|
||||
while abs(p - p0) > tol && iter < maxiter
|
||||
p0 = p
|
||||
p = aitken(f, p0)
|
||||
iter += 1
|
||||
end
|
||||
return abs(p - p0) > tol ? NaN : p
|
||||
end
|
||||
|
||||
""" deCasteljau function """
|
||||
function deCasteljau(c0, c1, c2, t)
|
||||
s = 1.0 - t
|
||||
return s * (s * c0 + t * c1) + t * (s * c1 + t * c2)
|
||||
end
|
||||
|
||||
xConvexLeftParabola(t) = deCasteljau(2, -8, 2, t)
|
||||
yConvexRightParabola(t) = deCasteljau(1, 2, 3, t)
|
||||
implicit_equation(x, y) = 5 * x^2 + y - 5
|
||||
|
||||
""" may return NaN on overflow """
|
||||
function f(t)
|
||||
return t in [nothing, NaN, Inf, -Inf] ? NaN :
|
||||
implicit_equation(xConvexLeftParabola(t), yConvexRightParabola(t)) + t
|
||||
end
|
||||
|
||||
""" test the example """
|
||||
function test_steffensen(tol = 0.00000001, iters = 1000, stepsize = 0.1)
|
||||
for t0 in 0:stepsize:1.1
|
||||
print("t0 = $t0 : ")
|
||||
t = steffensen_aitken(f, t0, tol, iters)
|
||||
if isnan(t)
|
||||
println("no answer")
|
||||
else
|
||||
x = xConvexLeftParabola(t)
|
||||
y = yConvexRightParabola(t)
|
||||
if abs(implicit_equation(x, y)) <= tol
|
||||
println("intersection at ($(Float32(x)), $(Float32(y)))")
|
||||
else
|
||||
println("spurious solution")
|
||||
end
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
test_steffensen()
|
||||
48
Task/Steffensens-method/Raku/steffensens-method.raku
Normal file
48
Task/Steffensens-method/Raku/steffensens-method.raku
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# 20230928 Raku programming solution
|
||||
|
||||
sub aitken($f, $p0) {
|
||||
my $p2 = $f( my $p1 = $f($p0) );
|
||||
my $p1m0 = $p1 - $p0;
|
||||
return $p0 - $p1m0*$p1m0/($p2-2.0*$p1+$p0);
|
||||
}
|
||||
|
||||
sub steffensenAitken($f, $pinit, $tol, $maxiter) {
|
||||
my ($iter, $p) = 1, aitken($f, my $p0 = $pinit);
|
||||
while abs($p-$p0) > $tol and $iter < $maxiter {
|
||||
$p = aitken($f, $p0 = $p);
|
||||
$iter++
|
||||
}
|
||||
return abs($p-$p0) > $tol ?? NaN !! $p
|
||||
}
|
||||
|
||||
sub deCasteljau($c0, $c1, $c2, $t) {
|
||||
my $s = 1.0 - $t;
|
||||
return $s*($s*$c0 + $t*$c1) + $t*($s*$c1 + $t*$c2)
|
||||
}
|
||||
|
||||
sub xConvexLeftParabola($t) { return deCasteljau(2.0, -8.0, 2.0, $t) }
|
||||
|
||||
sub yConvexRightParabola($t) { return deCasteljau(1.0, 2.0, 3.0, $t) }
|
||||
|
||||
sub implicitEquation($x, $y) { return 5.0*$x*$x + $y - 5.0 }
|
||||
|
||||
sub f($t) {
|
||||
implicitEquation(xConvexLeftParabola($t), yConvexRightParabola($t)) + $t
|
||||
}
|
||||
|
||||
my $t0 = 0.0;
|
||||
for ^11 {
|
||||
print "t0 = {$t0.fmt: '%0.1f'} : ";
|
||||
my $t = steffensenAitken(&f, $t0, 0.00000001, 1000);
|
||||
if $t.isNaN {
|
||||
say "no answer";
|
||||
} else {
|
||||
my ($x, $y) = xConvexLeftParabola($t), yConvexRightParabola($t);
|
||||
if abs(implicitEquation($x, $y)) <= 0.000001 {
|
||||
printf "intersection at (%f, %f)\n", $x, $y;
|
||||
} else {
|
||||
say "spurious solution";
|
||||
}
|
||||
}
|
||||
$t0 += 0.1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue