Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
10
Task/Numerical-integration/Ada/numerical-integration-1.adb
Normal file
10
Task/Numerical-integration/Ada/numerical-integration-1.adb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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;
|
||||
64
Task/Numerical-integration/Ada/numerical-integration-2.adb
Normal file
64
Task/Numerical-integration/Ada/numerical-integration-2.adb
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
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;
|
||||
104
Task/Numerical-integration/Ada/numerical-integration-3.adb
Normal file
104
Task/Numerical-integration/Ada/numerical-integration-3.adb
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
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;
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
script left_rect
|
||||
on call(f, x, h)
|
||||
f's call(x)
|
||||
end call
|
||||
end script
|
||||
|
||||
script right_rect
|
||||
on call(f, x, h)
|
||||
f's call(x + h)
|
||||
end call
|
||||
end script
|
||||
|
||||
script mid_rect
|
||||
on call(f, x, h)
|
||||
f's call(x + h / 2)
|
||||
end call
|
||||
end script
|
||||
|
||||
script trapezium
|
||||
on call(f, x, h)
|
||||
((f's call(x)) + (f's call(x + h))) / 2
|
||||
end call
|
||||
end script
|
||||
|
||||
script simpson
|
||||
on call(f, x, h)
|
||||
((f's call(x)) + 4 * (f's call(x + h / 2)) + (f's call(x + h))) / 6
|
||||
end call
|
||||
end script
|
||||
|
||||
on integrate(f, a, b, n, rule)
|
||||
set h to (b - a) / n
|
||||
set res to 0
|
||||
repeat with i from 0 to n - 1
|
||||
set res to res + (rule's call(f, a + i * h, h))
|
||||
end repeat
|
||||
return h * res
|
||||
end integrate
|
||||
|
||||
script cube
|
||||
on call(x)
|
||||
x ^ 3
|
||||
end call
|
||||
end script
|
||||
|
||||
script reciprocal
|
||||
on call(x)
|
||||
1 / x
|
||||
end call
|
||||
end script
|
||||
|
||||
script identity
|
||||
on call(x)
|
||||
x
|
||||
end call
|
||||
end script
|
||||
|
||||
on integral_test(f, a, b, n)
|
||||
log "Integrating " & f's name & " from " & a & " to " & b & " with " & n & " steps..."
|
||||
set rules to {left_rect, right_rect, mid_rect, trapezium, simpson}
|
||||
repeat with rule in rules
|
||||
log "Rule: " & rule's name
|
||||
log integrate(f, a, b, n, rule)
|
||||
end repeat
|
||||
end integral_test
|
||||
|
||||
integral_test(cube, 0, 1, 100)
|
||||
integral_test(reciprocal, 1, 100, 1000)
|
||||
integral_test(identity, 0, 5000, 5000000)
|
||||
integral_test(identity, 0, 6000, 6000000)
|
||||
129
Task/Numerical-integration/Chapel/numerical-integration-1.chpl
Normal file
129
Task/Numerical-integration/Chapel/numerical-integration-1.chpl
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
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();
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
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
|
||||
79
Task/Numerical-integration/Euphoria/numerical-integration.eu
Normal file
79
Task/Numerical-integration/Euphoria/numerical-integration.eu
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
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"))
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
const left_rect = (f, x, h) => f(x);
|
||||
const right_rect = (f, x, h) => f(x + h);
|
||||
const mid_rect = (f, x, h) => f(x + h / 2);
|
||||
const trapezium = (f, x, h) => (f(x) + f(x + h)) / 2;
|
||||
const simpson = (f, x, h) => (f(x) + 4 * f(x + h / 2) + f(x + h)) / 6;
|
||||
|
||||
function integrate(f, a, b, n, rule) {
|
||||
const h = (b - a) / n;
|
||||
let result = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
result += rule(f, a + i * h, h);
|
||||
}
|
||||
return h * result;
|
||||
}
|
||||
|
||||
function integral_test(f, a, b, n) {
|
||||
console.log(`Integrating ${f.toString()} from ${a} to ${b} with ${n} steps...`);
|
||||
const rules = {"Left rectangular method": left_rect,
|
||||
"Right rectangular method": right_rect,
|
||||
"Midpoint rectangular method": mid_rect,
|
||||
"Trapezium rule": trapezium,
|
||||
"Simpson's rule": simpson};
|
||||
|
||||
for (const r in rules) {
|
||||
console.log(`${r}: ${integrate(f, a, b, n, rules[r])}`);
|
||||
}
|
||||
console.log();
|
||||
}
|
||||
|
||||
integral_test(x => x ** 3, 0, 1, 100);
|
||||
integral_test(x => 1 / x, 1, 100, 1000);
|
||||
integral_test(x => x, 0, 5000, 5000000);
|
||||
integral_test(x => x, 0, 6000, 6000000);
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
-- 24 Aug 2025
|
||||
-- 25 Apr 2026
|
||||
include Setting
|
||||
arg digs
|
||||
if digs = '' then
|
||||
digs=9
|
||||
numeric digits digs
|
||||
|
||||
say 'NUMERICAL INTEGRATION: COMPARE 5 METHODS'
|
||||
say 'NUMERICAL INTEGRATION: COMPARE 6 METHODS'
|
||||
say version
|
||||
say
|
||||
w=Digits()+2
|
||||
|
|
@ -125,4 +125,5 @@ do n = 4 by 4 to steps-4
|
|||
end
|
||||
return (s0+32*s1+12*s2+14*s3)*2*h/45
|
||||
|
||||
-- Sin; Cos; Tan; Exp; Gamma
|
||||
include Math
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue