Numerical integration: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Common Lisp}}: Re-add task entry deleted for no apparent reason) |
(→{{header|PL/I}}: Rewritten to incorporate all the requirements.) |
||
Line 3,753: | Line 3,753: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
⚫ | |||
<lang PL/I> |
|||
⚫ | |||
f: procedure (x, function) returns (float(18)); |
|||
declare x float(18), function fixed binary; |
|||
f: procedure (x) returns (float); |
|||
select (function); |
|||
return ( |
when (1) return (x**3); |
||
when (2) return (1/x); |
|||
when (3) return (x); |
|||
when (4) return (x); |
|||
end; |
|||
end f; |
end f; |
||
declare (a, b) |
declare (a, b) fixed decimal (10); |
||
declare (rect_area, trap_area, Simpson) float; |
declare (rect_area, trap_area, Simpson) float(18); |
||
declare (d, dx) |
declare (d, dx) float(18); |
||
declare ( |
declare (S1, S2) float(18); |
||
declare ( |
declare N fixed decimal (15), function fixed binary; |
||
declare k fixed decimal (7,2); |
|||
put (' Rectangle-left Rectangle-mid Rectangle-right' || |
|||
⚫ | |||
' Trapezoid Simpson'); |
|||
a = 0; b = 5; /* bounds of integration */ |
|||
do function = 1 to 4; |
|||
select(function); |
|||
when (1) do; N = 100; a = 0; b = 1; end; |
|||
when (2) do; N = 1000; a = 1; b = 100; end; |
|||
when (3) do; N = 5000000; a = 0; b = 5000; end; |
|||
when (4) do; N = 6000000; a = 0; b = 6000; end; |
|||
⚫ | |||
dx = (b-a)/float(N); |
|||
/* Rectangle method */ |
/* Rectangle method, left-side */ |
||
rect_area = 0; |
rect_area = 0; |
||
do d = |
do d = 0 to N-1; |
||
rect_area = rect_area + dx*f(d); |
rect_area = rect_area + dx*f(a + d*dx, function); |
||
end; |
end; |
||
put skip |
put skip edit (rect_area) (E(25, 15)); |
||
/* |
/* Rectangle method, mid-point */ |
||
rect_area = 0; |
|||
do d = |
do d = 0 to N-1; |
||
rect_area = rect_area + dx*f(a + d*dx + dx/2, function); |
|||
end; |
end; |
||
put |
put edit (rect_area) (E(25, 15)); |
||
/* Rectangle method, right-side */ |
|||
rect_area = 0; |
|||
do d = 1 to N; |
|||
rect_area = rect_area + dx*f(a + d*dx, function); |
|||
end; |
|||
put edit (rect_area) (E(25, 15)); |
|||
/* Trapezoid method */ |
|||
trap_area = 0; |
|||
do d = 0 to N-1; |
|||
trap_area = trap_area + dx*(f(a+d*dx, function) + f(a+(d+1)*dx, function))/2; |
|||
end; |
|||
put edit (trap_area) (X(1), E(25, 15)); |
|||
/* Simpson's */ |
/* Simpson's Rule */ |
||
S1 = f(a+dx/2); |
S1 = f(a+dx/2, function); |
||
S2 = 0; |
S2 = 0; |
||
do d = |
do d = 1 to N-1; |
||
S1 = S1 + f( |
S1 = S1 + f(a+d*dx+dx/2, function); |
||
S2 = S2 + f( |
S2 = S2 + f(a+d*dx, function); |
||
end; |
|||
⚫ | |||
put edit (Simpson) (X(1), E(25, 15)); |
|||
end; |
end; |
||
⚫ | |||
put skip data (Simpson); |
|||
end integrals; |
end integrals; |
||
</lang> |
</lang> |
||
<pre> |
|||
Rectangle-left Rectangle-mid Rectangle-right Trapezoid Simpson |
|||
2.450250000000000E-0001 2.499875000000000E-0001 2.550250000000000E-0001 2.500250000000000E-0001 2.500000000000000E-0001 |
|||
4.654991057514676E+0000 4.604762548678375E+0000 4.556981057514676E+0000 4.605986057514676E+0000 4.605170384957142E+0000 |
|||
1.249999750000000E+0007 1.250000000000000E+0007 1.250000250000000E+0007 1.250000000000000E+0007 1.250000000000000E+0007 |
|||
1.799999700000000E+0007 1.800000000000000E+0007 1.800000300000000E+0007 1.800000000000000E+0007 1.800000000000000E+0007 |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |