Numerical integration: Difference between revisions

Content deleted Content added
m →‎{{header|Python}}: Aargh! should be good now.
→‎{{header|Perl 6}}: update to new testing regime
Line 1,307:
}
sub tryem(&f(, $x)a, {$b, 2 / (1 + 4*$x**2n); }{
say "rectangle method rightleft: { rightrect", leftrect &f, $a, $b, $n }";
sub g($x) { atan(2*$x); }
say "rectangle method midright: ", { midrectrightrect &f, $a, $b, $n }";
say "rectangle method mid: ", midrect &f, $a, $b, $n;
my ($a, $b, $n) = (-1, 2, 4);
say "composite trapezoidal rule: ", trapez &f, $a, $b, $n;
say "rectanglequadratic methodsimpsons leftrule: ", simpsons { leftrect &f, $a, $b, $n }";
}
say "rectangle method right: { rightrect &f, $a, $b, $n }";
 
say "rectangle method mid: { midrect &f, $a, $b, $n }";
say "composite'-> trapezoidal rule:$x { trapez2 &f,/ $a,(1 $b,+ 4 * $nx ** 2) }"';
saytryem "quadratic-> simpsons$x rule:{ 2 / (1 {+ simpsons4 &f,* $a,x $b,* $nx) }", -1, 2, 4;
 
say " ∫f(x) in [$a,$b]:".fmt("%-30s"), g($b) - g($a);</lang>
say "\n",'{ $_ ** 3 }';
tryem { $_ * $_ * $_ }, 0, 1, 100; # rakudo doesn't do Rat ** Int --> Rat yet
 
say "\n",'1 / *';
tryem 1 / *, 1, 100, 1000;</lang>
Output:
<lang>rectangle-> method$x left:{ 2 / (1 + 4 * $x ** 2.45689655172414) }
rectangle method rightleft: 2.2451318458417945689655172414
rectangle method right: 2.24513184584178
rectangle method mid: 2.49609105850139
composite trapezoidal rule: 2.35101419878296
quadratic simpsons rule: 2.44773210526191
∫f(x) in [-1,2]: 2.43296638146212</lang>
 
{ $_ ** 3 }
Note that these integrations are done with rationals rather than floats, so should be fairly precise (though of course they are not terribly accurate with so few iterations).
rectangle method left: 0.245025
rectangle method right: 0.255025
rectangle method mid: 0.2499875
composite trapezoidal rule: 0.250025
quadratic simpsons rule: 0.25
 
1 / *
rectangle method left: 4.65499105751468
rectangle method right: 4.55698105751468
rectangle method mid: 4.60476254867838
composite trapezoidal rule: 4.60598605751468
quadratic simpsons rule: 4.60517038495714</lang>
 
Note that these integrations are done with rationals rather than floats, so should be fairly precise (though of course they are not terribly accurate with so few iterations). Some of the sums do overflow into Num (floating point)--currently rakudo allows implements Rat32--but at least all of the interval arithmetic is exact.
 
=={{header|PL/I}}==