Numerical integration: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1,462:
}
}</lang>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Python}}
<lang Delphi>program Numerical_integration;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
type
TFx = TFunc<Double, Double>;
 
TMethod = TFunc<TFx, Double, Double, Double>;
 
function RectLeft(f: TFx; x, h: Double): Double;
begin
RectLeft := f(x);
end;
 
function RectMid(f: TFx; x, h: Double): Double;
begin
RectMid := f(x + h / 2);
end;
 
function RectRight(f: TFx; x, h: Double): Double;
begin
Result := f(x + h);
end;
 
function Trapezium(f: TFx; x, h: Double): Double;
begin
Result := (f(x) + f(x + h)) / 2.0;
end;
 
function Simpson(f: TFx; x, h: Double): Double;
begin
Result := (f(x) + 4 * f(x + h / 2) + f(x + h)) / 6.0;
end;
 
function Integrate(Method: TMethod; f: TFx; a, b: Double; n: Integer): Double;
var
h: Double;
k: integer;
begin
Result := 0;
h := (b - a) / n;
for k := 0 to n - 1 do
Result := Result + Method(f, a + k * h, h);
Result := Result * h;
end;
 
function f1(x: Double): Double;
begin
Result := x * x * x;
end;
 
function f2(x: Double): Double;
begin
Result := 1 / x;
end;
 
function f3(x: Double): Double;
begin
Result := x;
end;
 
var
fs: array[0..3] of TFx;
mt: array[0..4] of TMethod;
fsNames: array of string = ['x^3', '1/x', 'x', 'x'];
mtNames: array of string = ['RectLeft', 'RectMid', 'RectRight', 'Trapezium', 'Simpson'];
limits: array of array of Double = [[0, 1, 100], [1, 100, 1000], [0, 5000,
5000000], [0, 6000, 6000000]];
i, j, n: integer;
a, b: double;
 
begin
fs[0] := f1;
fs[1] := f2;
fs[2] := f3;
fs[3] := f3;
 
mt[0] := RectLeft;
mt[1] := RectMid;
mt[2] := RectRight;
mt[3] := Trapezium;
mt[4] := Simpson;
 
for i := 0 to High(fs) do
begin
Writeln('Integrate ' + fsNames[i]);
a := limits[i][0];
b := limits[i][1];
n := Trunc(limits[i][2]);
 
for j := 0 to High(mt) do
Writeln(Format('%.6f', [Integrate(mt[j], fs[i], a, b, n)]));
end;
readln;
end.</lang>
{{out}}
<pre>Integrate x^3
0,245025
0,249988
0,255025
0,250025
0,250000
Integrate 1/x
4,654991
4,604763
4,556981
4,605986
4,605170
Integrate x
12499997,500000
12500000,000000
12500002,500000
12500000,000000
12500000,000000
Integrate x
17999997,000000
18000000,000000
18000003,000000
18000000,000000
18000000,000000</pre>
=={{header|E}}==
 
478

edits