Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,10 +0,0 @@
|
|||
generic
|
||||
type Scalar is digits <>;
|
||||
with function F (X : Scalar) return Scalar;
|
||||
package Integrate is
|
||||
function Left_Rectangular (A, B : Scalar; N : Positive) return Scalar;
|
||||
function Right_Rectangular (A, B : Scalar; N : Positive) return Scalar;
|
||||
function Midpoint_Rectangular (A, B : Scalar; N : Positive) return Scalar;
|
||||
function Trapezium (A, B : Scalar; N : Positive) return Scalar;
|
||||
function Simpsons (A, B : Scalar; N : Positive) return Scalar;
|
||||
end Integrate;
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
package body Integrate is
|
||||
function Left_Rectangular (A, B : Scalar; N : Positive) return Scalar is
|
||||
H : constant Scalar := (B - A) / Scalar (N);
|
||||
Sum : Scalar := 0.0;
|
||||
X : Scalar;
|
||||
begin
|
||||
for I in 0 .. N - 1 loop
|
||||
X := A + Scalar (I) * H;
|
||||
Sum := Sum + H * F (X);
|
||||
end loop;
|
||||
return Sum;
|
||||
end Left_Rectangular;
|
||||
|
||||
function Right_Rectangular (A, B : Scalar; N : Positive) return Scalar is
|
||||
H : constant Scalar := (B - A) / Scalar (N);
|
||||
Sum : Scalar := 0.0;
|
||||
X : Scalar;
|
||||
begin
|
||||
for I in 1 .. N loop
|
||||
X := A + Scalar (I) * H;
|
||||
Sum := Sum + H * F (X);
|
||||
end loop;
|
||||
return Sum;
|
||||
end Right_Rectangular;
|
||||
|
||||
function Midpoint_Rectangular (A, B : Scalar; N : Positive) return Scalar is
|
||||
H : constant Scalar := (B - A) / Scalar (N);
|
||||
Sum : Scalar := 0.0;
|
||||
X : Scalar;
|
||||
begin
|
||||
for I in 1 .. N loop
|
||||
X := A + Scalar (I) * H - 0.5 * H;
|
||||
Sum := Sum + H * F (X);
|
||||
end loop;
|
||||
return Sum;
|
||||
end Midpoint_Rectangular;
|
||||
|
||||
function Trapezium (A, B : Scalar; N : Positive) return Scalar is
|
||||
H : constant Scalar := (B - A) / Scalar (N);
|
||||
Sum : Scalar := F(A) + F(B);
|
||||
X : Scalar := 1.0;
|
||||
begin
|
||||
while X <= Scalar (N) - 1.0 loop
|
||||
Sum := Sum + 2.0 * F (A + X * (B - A) / Scalar (N));
|
||||
X := X + 1.0;
|
||||
end loop;
|
||||
return (B - A) / (2.0 * Scalar (N)) * Sum;
|
||||
end Trapezium;
|
||||
|
||||
function Simpsons (A, B : Scalar; N : Positive) return Scalar is
|
||||
H : constant Scalar := (B - A) / Scalar (N);
|
||||
Sum_U : Scalar := 0.0;
|
||||
Sum_E : Scalar := 0.0;
|
||||
begin
|
||||
for I in 1 .. N - 1 loop
|
||||
if I mod 2 /= 0 then
|
||||
Sum_U := Sum_U + F (A + H * Scalar (I));
|
||||
else
|
||||
Sum_E := Sum_E + F (A + H * Scalar (I));
|
||||
end if;
|
||||
end loop;
|
||||
return (H / 3.0) * (F (A) + F (B) + 4.0 * Sum_U + 2.0 * Sum_E);
|
||||
end Simpsons;
|
||||
end Integrate;
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
with Ada.Text_IO, Ada.Integer_Text_IO;
|
||||
with Integrate;
|
||||
|
||||
procedure Numerical_Integration is
|
||||
type Scalar is digits 18;
|
||||
package Scalar_Text_IO is new Ada.Text_IO.Float_IO (Scalar);
|
||||
|
||||
generic
|
||||
with function F (X : Scalar) return Scalar;
|
||||
Name : String;
|
||||
From, To : Scalar;
|
||||
Steps : Positive;
|
||||
procedure Test;
|
||||
|
||||
procedure Test is
|
||||
package Integrate_Scalar_F is new Integrate (Scalar, F);
|
||||
use Ada.Text_IO, Ada.Integer_Text_IO, Integrate_Scalar_F, Scalar_Text_IO;
|
||||
begin
|
||||
Put (Name & " integrated from ");
|
||||
Put (From);
|
||||
Put (" to ");
|
||||
Put (To);
|
||||
Put (" in ");
|
||||
Put (Steps);
|
||||
Put_Line (" steps:");
|
||||
|
||||
Put ("Rectangular (left): ");
|
||||
Put (Left_Rectangular (From, To, Steps));
|
||||
New_Line;
|
||||
|
||||
Put ("Rectangular (right): ");
|
||||
Put (Right_Rectangular (From, To, Steps));
|
||||
New_Line;
|
||||
|
||||
Put ("Rectangular (midpoint): ");
|
||||
Put (Midpoint_Rectangular (From, To, Steps));
|
||||
New_Line;
|
||||
|
||||
Put ("Trapezium: ");
|
||||
Put (Trapezium (From, To, Steps));
|
||||
New_Line;
|
||||
|
||||
Put ("Simpson's: ");
|
||||
Put (Simpsons (From, To, Steps));
|
||||
New_Line;
|
||||
|
||||
New_Line;
|
||||
end Test;
|
||||
begin
|
||||
Ada.Integer_Text_IO.Default_Width := 0;
|
||||
Scalar_Text_IO.Default_Fore := 0;
|
||||
Scalar_Text_IO.Default_Exp := 0;
|
||||
|
||||
Cubed:
|
||||
declare
|
||||
function F (X : Scalar) return Scalar is
|
||||
begin
|
||||
return X ** 3;
|
||||
end F;
|
||||
procedure Run is new Test (F => F,
|
||||
Name => "x^3",
|
||||
From => 0.0,
|
||||
To => 1.0,
|
||||
Steps => 100);
|
||||
begin
|
||||
Run;
|
||||
end Cubed;
|
||||
|
||||
One_Over_X:
|
||||
declare
|
||||
function F (X : Scalar) return Scalar is
|
||||
begin
|
||||
return 1.0 / X;
|
||||
end F;
|
||||
procedure Run is new Test (F => F,
|
||||
Name => "1/x",
|
||||
From => 1.0,
|
||||
To => 100.0,
|
||||
Steps => 1_000);
|
||||
begin
|
||||
Run;
|
||||
end One_Over_X;
|
||||
|
||||
X:
|
||||
declare
|
||||
function F (X : Scalar) return Scalar is
|
||||
begin
|
||||
return X;
|
||||
end F;
|
||||
procedure Run_1 is new Test (F => F,
|
||||
Name => "x",
|
||||
From => 0.0,
|
||||
To => 5_000.0,
|
||||
Steps => 5_000_000);
|
||||
procedure Run_2 is new Test (F => F,
|
||||
Name => "x",
|
||||
From => 0.0,
|
||||
To => 6_000.0,
|
||||
Steps => 6_000_000);
|
||||
begin
|
||||
Run_1;
|
||||
Run_2;
|
||||
end X;
|
||||
end Numerical_Integration;
|
||||
|
|
@ -1,129 +0,0 @@
|
|||
proc f1(x:real):real {
|
||||
return x**3;
|
||||
}
|
||||
|
||||
proc f2(x:real):real {
|
||||
return 1/x;
|
||||
}
|
||||
|
||||
proc f3(x:real):real {
|
||||
return x;
|
||||
}
|
||||
|
||||
proc leftRectangleIntegration(a: real, b: real, N: int, f): real{
|
||||
var h: real = (b - a)/N;
|
||||
var sum: real = 0.0;
|
||||
var x_n: real;
|
||||
for n in 0..N-1 {
|
||||
x_n = a + n * h;
|
||||
sum = sum + f(x_n);
|
||||
}
|
||||
return h * sum;
|
||||
}
|
||||
|
||||
proc rightRectangleIntegration(a: real, b: real, N: int, f): real{
|
||||
var h: real = (b - a)/N;
|
||||
var sum: real = 0.0;
|
||||
var x_n: real;
|
||||
for n in 0..N-1 {
|
||||
x_n = a + (n + 1) * h;
|
||||
sum = sum + f(x_n);
|
||||
}
|
||||
return h * sum;
|
||||
}
|
||||
|
||||
proc midpointRectangleIntegration(a: real, b: real, N: int, f): real{
|
||||
var h: real = (b - a)/N;
|
||||
var sum: real = 0.0;
|
||||
var x_n: real;
|
||||
for n in 0..N-1 {
|
||||
x_n = a + (n + 0.5) * h;
|
||||
sum = sum + f(x_n);
|
||||
}
|
||||
return h * sum;
|
||||
}
|
||||
|
||||
proc trapezoidIntegration(a: real(64), b: real(64), N: int(64), f): real{
|
||||
var h: real(64) = (b - a)/N;
|
||||
var sum: real(64) = f(a) + f(b);
|
||||
var x_n: real(64);
|
||||
for n in 1..N-1 {
|
||||
x_n = a + n * h;
|
||||
sum = sum + 2.0 * f(x_n);
|
||||
}
|
||||
return (h/2.0) * sum;
|
||||
}
|
||||
|
||||
proc simpsonsIntegration(a: real(64), b: real(64), N: int(64), f): real{
|
||||
var h: real(64) = (b - a)/N;
|
||||
var sum: real(64) = f(a) + f(b);
|
||||
var x_n: real(64);
|
||||
for n in 1..N-1 by 2 {
|
||||
x_n = a + n * h;
|
||||
sum = sum + 4.0 * f(x_n);
|
||||
}
|
||||
for n in 2..N-2 by 2 {
|
||||
x_n = a + n * h;
|
||||
sum = sum + 2.0 * f(x_n);
|
||||
}
|
||||
return (h/3.0) * sum;
|
||||
}
|
||||
|
||||
var exact:real;
|
||||
var calculated:real;
|
||||
|
||||
writeln("f(x) = x**3 with 100 steps from 0 to 1");
|
||||
exact = 0.25;
|
||||
calculated = leftRectangleIntegration(a = 0.0, b = 1.0, N = 100, f = f1);
|
||||
writeln("leftRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = rightRectangleIntegration(a = 0.0, b = 1.0, N = 100, f = f1);
|
||||
writeln("rightRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = midpointRectangleIntegration(a = 0.0, b = 1.0, N = 100, f = f1);
|
||||
writeln("midpointRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = trapezoidIntegration(a = 0.0, b = 1.0, N = 100, f = f1);
|
||||
writeln("trapezoidIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = simpsonsIntegration(a = 0.0, b = 1.0, N = 100, f = f1);
|
||||
writeln("simpsonsIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
writeln();
|
||||
|
||||
writeln("f(x) = 1/x with 1000 steps from 1 to 100");
|
||||
exact = 4.605170;
|
||||
calculated = leftRectangleIntegration(a = 1.0, b = 100.0, N = 1000, f = f2);
|
||||
writeln("leftRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = rightRectangleIntegration(a = 1.0, b = 100.0, N = 1000, f = f2);
|
||||
writeln("rightRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = midpointRectangleIntegration(a = 1.0, b = 100.0, N = 1000, f = f2);
|
||||
writeln("midpointRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = trapezoidIntegration(a = 1.0, b = 100.0, N = 1000, f = f2);
|
||||
writeln("trapezoidIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = simpsonsIntegration(a = 1.0, b = 100.0, N = 1000, f = f2);
|
||||
writeln("simpsonsIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
writeln();
|
||||
|
||||
writeln("f(x) = x with 5000000 steps from 0 to 5000");
|
||||
exact = 12500000;
|
||||
calculated = leftRectangleIntegration(a = 0.0, b = 5000.0, N = 5000000, f = f3);
|
||||
writeln("leftRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = rightRectangleIntegration(a = 0.0, b = 5000.0, N = 5000000, f = f3);
|
||||
writeln("rightRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = midpointRectangleIntegration(a = 0.0, b = 5000.0, N = 5000000, f = f3);
|
||||
writeln("midpointRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = trapezoidIntegration(a = 0.0, b = 5000.0, N = 5000000, f = f3);
|
||||
writeln("trapezoidIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = simpsonsIntegration(a = 0.0, b = 5000.0, N = 5000000, f = f3);
|
||||
writeln("simpsonsIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
writeln();
|
||||
|
||||
writeln("f(x) = x with 6000000 steps from 0 to 6000");
|
||||
exact = 18000000;
|
||||
calculated = leftRectangleIntegration(a = 0.0, b = 6000.0, N = 6000000, f = f3);
|
||||
writeln("leftRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = rightRectangleIntegration(a = 0.0, b = 6000.0, N = 6000000, f = f3);
|
||||
writeln("rightRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = midpointRectangleIntegration(a = 0.0, b = 6000.0, N = 6000000, f = f3);
|
||||
writeln("midpointRectangleIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = trapezoidIntegration(a = 0.0, b = 6000.0, N = 6000000, f = f3);
|
||||
writeln("trapezoidIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
calculated = simpsonsIntegration(a = 0.0, b = 6000.0, N = 6000000, f = f3);
|
||||
writeln("simpsonsIntegration: calculated = ", calculated, "; exact = ", exact, "; difference = ", abs(calculated - exact));
|
||||
writeln();
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
f(x) = x**3 with 100 steps from 0 to 1
|
||||
leftRectangleIntegration: calculated = 0.245025; exact = 0.25; difference = 0.004975
|
||||
rightRectangleIntegration: calculated = 0.255025; exact = 0.25; difference = 0.005025
|
||||
midpointRectangleIntegration: calculated = 0.249988; exact = 0.25; difference = 1.25e-05
|
||||
trapezoidIntegration: calculated = 0.250025; exact = 0.25; difference = 2.5e-05
|
||||
simpsonsIntegration: calculated = 0.25; exact = 0.25; difference = 5.55112e-17
|
||||
|
||||
f(x) = 1/x with 1000 steps from 1 to 100
|
||||
leftRectangleIntegration: calculated = 4.65499; exact = 4.60517; difference = 0.0498211
|
||||
rightRectangleIntegration: calculated = 4.55698; exact = 4.60517; difference = 0.0481889
|
||||
midpointRectangleIntegration: calculated = 4.60476; exact = 4.60517; difference = 0.000407451
|
||||
trapezoidIntegration: calculated = 4.60599; exact = 4.60517; difference = 0.000816058
|
||||
simpsonsIntegration: calculated = 4.60517; exact = 4.60517; difference = 3.31627e-06
|
||||
|
||||
f(x) = x with 5000000 steps from 0 to 5000
|
||||
leftRectangleIntegration: calculated = 1.25e+07; exact = 1.25e+07; difference = 2.5
|
||||
rightRectangleIntegration: calculated = 1.25e+07; exact = 1.25e+07; difference = 2.5
|
||||
midpointRectangleIntegration: calculated = 1.25e+07; exact = 1.25e+07; difference = 0.0
|
||||
trapezoidIntegration: calculated = 1.25e+07; exact = 1.25e+07; difference = 1.86265e-09
|
||||
simpsonsIntegration: calculated = 1.25e+07; exact = 1.25e+07; difference = 3.72529e-09
|
||||
|
||||
f(x) = x with 6000000 steps from 0 to 6000
|
||||
leftRectangleIntegration: calculated = 1.8e+07; exact = 1.8e+07; difference = 3.0
|
||||
rightRectangleIntegration: calculated = 1.8e+07; exact = 1.8e+07; difference = 3.0
|
||||
midpointRectangleIntegration: calculated = 1.8e+07; exact = 1.8e+07; difference = 7.45058e-09
|
||||
trapezoidIntegration: calculated = 1.8e+07; exact = 1.8e+07; difference = 3.72529e-09
|
||||
simpsonsIntegration: calculated = 1.8e+07; exact = 1.8e+07; difference = 0.0
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
function int_leftrect(sequence bounds, integer n, integer func_id)
|
||||
atom h, sum
|
||||
h = (bounds[2]-bounds[1])/n
|
||||
sum = 0
|
||||
for x = bounds[1] to bounds[2]-h by h do
|
||||
sum += call_func(func_id, {x})
|
||||
end for
|
||||
return h*sum
|
||||
end function
|
||||
|
||||
function int_rightrect(sequence bounds, integer n, integer func_id)
|
||||
atom h, sum
|
||||
h = (bounds[2]-bounds[1])/n
|
||||
sum = 0
|
||||
for x = bounds[1] to bounds[2]-h by h do
|
||||
sum += call_func(func_id, {x+h})
|
||||
end for
|
||||
return h*sum
|
||||
end function
|
||||
|
||||
function int_midrect(sequence bounds, integer n, integer func_id)
|
||||
atom h, sum
|
||||
h = (bounds[2]-bounds[1])/n
|
||||
sum = 0
|
||||
for x = bounds[1] to bounds[2]-h by h do
|
||||
sum += call_func(func_id, {x+h/2})
|
||||
end for
|
||||
return h*sum
|
||||
end function
|
||||
|
||||
function int_trapezium(sequence bounds, integer n, integer func_id)
|
||||
atom h, sum
|
||||
h = (bounds[2]-bounds[1])/n
|
||||
sum = call_func(func_id, {bounds[1]}) + call_func(func_id, {bounds[2]})
|
||||
for x = bounds[1] to bounds[2]-h by h do
|
||||
sum += 2*call_func(func_id, {x})
|
||||
end for
|
||||
return h * sum / 2
|
||||
end function
|
||||
|
||||
function int_simpson(sequence bounds, integer n, integer func_id)
|
||||
atom h, sum1, sum2
|
||||
h = (bounds[2]-bounds[1])/n
|
||||
sum1 = call_func(func_id, {bounds[1] + h/2})
|
||||
sum2 = 0
|
||||
for i = 1 to n-1 do
|
||||
sum1 += call_func(func_id, {bounds[1] + h * i + h / 2})
|
||||
sum2 += call_func(func_id, {bounds[1] + h * i})
|
||||
end for
|
||||
return h/6 * (call_func(func_id, {bounds[1]}) +
|
||||
call_func(func_id, {bounds[2]}) + 4*sum1 + 2*sum2)
|
||||
end function
|
||||
|
||||
function xp2d2(atom x)
|
||||
return x*x/2
|
||||
end function
|
||||
|
||||
function logx(atom x)
|
||||
return log(x)
|
||||
end function
|
||||
|
||||
function x(atom x)
|
||||
return x
|
||||
end function
|
||||
|
||||
? int_leftrect({-1,1},1000,routine_id("xp2d2"))
|
||||
? int_rightrect({-1,1},1000,routine_id("xp2d2"))
|
||||
? int_midrect({-1,1},1000,routine_id("xp2d2"))
|
||||
? int_simpson({-1,1},1000,routine_id("xp2d2"))
|
||||
puts(1,'\n')
|
||||
? int_leftrect({1,2},1000,routine_id("logx"))
|
||||
? int_rightrect({1,2},1000,routine_id("logx"))
|
||||
? int_midrect({1,2},1000,routine_id("logx"))
|
||||
? int_simpson({1,2},1000,routine_id("logx"))
|
||||
puts(1,'\n')
|
||||
? int_leftrect({0,10},1000,routine_id("x"))
|
||||
? int_rightrect({0,10},1000,routine_id("x"))
|
||||
? int_midrect({0,10},1000,routine_id("x"))
|
||||
? int_simpson({0,10},1000,routine_id("x"))
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
-- 28 Jul 2025
|
||||
include Settings
|
||||
-- 24 Aug 2025
|
||||
include Setting
|
||||
arg digs
|
||||
if digs = '' then
|
||||
digs=9
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue