24 lines
828 B
Text
24 lines
828 B
Text
fcn integrate(F,f,a,b,steps){
|
|
h:=(b - a) / steps;
|
|
h*(0).reduce(steps,'wrap(s,i){ F(f, h*i + a, h) + s },0.0);
|
|
}
|
|
|
|
fcn rectangularLeft(f,x) { f(x) }
|
|
fcn rectangularMiddle(f,x,h){ f(x+h/2) }
|
|
fcn rectangularRight(f,x,h) { f(x+h) }
|
|
fcn trapezium(f,x,h) { (f(x) + f(x+h))/2 }
|
|
fcn simpson(f,x,h) { (f(x) + 4.0*f(x+h/2) + f(x+h))/6 }
|
|
|
|
args:=T( T(fcn(x){ x.pow(3) }, 0.0, 1.0, 10),
|
|
T(fcn(x){ 1.0 / x }, 1.0, 100.0, 1000),
|
|
T(fcn(x){ x }, 0.0, 5000.0, 0d5_000_000),
|
|
T(fcn(x){ x }, 0.0, 6000.0, 0d6_000_000) );
|
|
fs:=T(rectangularLeft,rectangularMiddle,rectangularRight,
|
|
trapezium,simpson);
|
|
names:=fs.pump(List,"name",'+(":"),"%-18s".fmt);
|
|
|
|
foreach a in (args){
|
|
names.zipWith('wrap(nm,f){
|
|
"%s %f".fmt(nm,integrate(f,a.xplode())).println() }, fs);
|
|
println();
|
|
}
|