Numerical integration: Difference between revisions
Content added Content deleted
m (→{{header|FreeBASIC}}: added some info) |
(→{{header|Perl 6}}: modernize) |
||
Line 3,199:
=={{header|Perl 6}}==
{{works with|Rakudo|
<lang perl6>sub leftrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
$h * [+] do f($_) for $a,
}
sub rightrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
$h * [+] do f($_) for $a+$h,
}
sub midrect(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
$h * [+] do f($_) for $a+$h/2,
}
sub trapez(&f, $a, $b, $n) {
my $h = ($b - $a) / $n;
$h / 2 * [+] f($a), f($b), |do f($_) * 2 for $a+$h,
}
Line 3,236:
sub tryem($f, $a, $b, $n, $exact) {
say "\n$f\n in [$a..$b] / $n";
say ' exact result: ', $exact;
say ' rectangle method left: ', leftrect &f, $a, $b, $n;
Line 3,244:
say ' quadratic simpsons rule: ', simpsons &f, $a, $b, $n;"
}
tryem '{ $_ ** 3 }', 0, 1, 100, 0.25;
tryem '1 / *', 1, 100, 1000, log(100);
tryem '
tryem '
{{out}}
<lang>{ $_ ** 3 }
in [0..1] / 100
Line 3,261 ⟶ 3,258:
rectangle method left: 0.245025
rectangle method right: 0.255025
rectangle method mid: 0.
composite trapezoidal rule: 0.250025
quadratic simpsons rule: 0.25
Line 3,274 ⟶ 3,271:
quadratic simpsons rule: 4.60517038495714
*.self
in [0..5000] /
exact result: 12500000
rectangle method left:
rectangle method right:
rectangle method mid: 12500000
composite trapezoidal rule: 12500000
quadratic simpsons rule: 12500000
*.self
in [0..6000] /
exact result: 18000000
rectangle method left:
rectangle method right:
rectangle method mid: 18000000
composite trapezoidal rule: 18000000
quadratic simpsons rule: 18000000</lang>
Note that these integrations are done with rationals rather than floats, so should be fairly precise (though of course with so few iterations they are not terribly accurate (except when they are)). Some of the sums do overflow into Num (floating point)--currently rakudo allows
=={{header|PL/I}}==
|