How to resolve the algorithm Numerical integration step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Numerical integration step by step in the Delphi programming language
Table of Contents
Problem Statement
Write functions to calculate the definite integral of a function ƒ(x) using all five of the following methods: Your functions should take in the upper and lower bounds (a and b), and the number of approximations to make in that range (n). Assume that your example already has a function that gives values for ƒ(x) . Simpson's method is defined by the following pseudo-code:
Demonstrate your function by showing the results for:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numerical integration step by step in the Delphi programming language
Source code in the delphi programming language
program Numerical_integration;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TFx = TFunc;
TMethod = TFunc;
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.
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Leonardo numbers step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Gamma function step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Program name step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Rascal programming language