Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
129
Task/Numerical-integration/Chapel/numerical-integration-1.chapel
Normal file
129
Task/Numerical-integration/Chapel/numerical-integration-1.chapel
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
|
||||
|
|
@ -16,45 +16,44 @@ numeric digits 20 /*use twenty digits precision. */
|
|||
end /*test*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────LEFT_RECT subroutine────────────────*/
|
||||
left_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
left_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
sum=0
|
||||
do x=a by h for n
|
||||
sum=sum+f(x)
|
||||
end
|
||||
end /*x*/
|
||||
return sum*h
|
||||
/*──────────────────────────────────MIDPOINT_RECT subroutine────────────*/
|
||||
midpoint_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
midpoint_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
sum=0
|
||||
do x=a+h/2 by h for n
|
||||
sum=sum+f(x)
|
||||
end
|
||||
end /*x*/
|
||||
return sum*h
|
||||
/*──────────────────────────────────RIGHT_RECT subroutine───────────────*/
|
||||
right_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
right_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
sum=0
|
||||
do x=a+h by h for n
|
||||
sum=sum+f(x)
|
||||
end
|
||||
end /*x*/
|
||||
return sum*h
|
||||
/*──────────────────────────────────SIMPSON subroutine──────────────────*/
|
||||
simpson: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
simpson: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
sum1=f(a+h/2)
|
||||
sum2=0
|
||||
do x=1 to n-1
|
||||
sum2=0; do x=1 to n-1
|
||||
sum1=sum1+f(a+h*x+h*.5)
|
||||
sum2=sum2+f(a+x*h)
|
||||
end
|
||||
end /*x*/
|
||||
|
||||
return h*(f(a)+f(b)+4*sum1+2*sum2)/6
|
||||
/*──────────────────────────────────TRAPEZOID subroutine────────────────*/
|
||||
trapezoid: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
trapezoid: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
sum=0
|
||||
do x=a to b by h
|
||||
sum=sum+h*(f(x)+f(x+h))*.5
|
||||
end
|
||||
end /*x*/
|
||||
return sum
|
||||
/*──────────────────────────────────F subroutine────────────────────────*/
|
||||
f: procedure expose test; parse arg z
|
||||
if test==1 then return z**3
|
||||
if test==2 then return 1/z
|
||||
return z
|
||||
f: procedure expose test; parse arg z
|
||||
if test==1 then return z**3
|
||||
if test==2 then return 1/z
|
||||
return z
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue