Numerical integration/Adaptive Simpson's method: Difference between revisions
Content added Content deleted
(Added Ada before →{{header|ALGOL 60}}) |
(Awk before C) |
||
Line 560: | Line 560: | ||
estimate of ∫ sin x dx from 0 to 1, by call to a compiled function: 0.459698 |
estimate of ∫ sin x dx from 0 to 1, by call to a compiled function: 0.459698 |
||
estimate of ∫ sin x dx from 0 to 1, by call to the template: 0.459698</pre> |
estimate of ∫ sin x dx from 0 to 1, by call to the template: 0.459698</pre> |
||
=={{header|Awk}}== |
|||
<syntaxhighlight lang="awk"> |
|||
BEGIN { |
|||
printf ("estimate of ∫ sin x dx from 0 to 1: %f\n", |
|||
quad_asr(0.0, 1.0, 1.0e-9, 100)) |
|||
} |
|||
function f(x) |
|||
{ |
|||
return sin(x) |
|||
} |
|||
function midpoint(a, b) |
|||
{ |
|||
return 0.5 * (a + b) |
|||
} |
|||
function simpson_rule(a, fa, b, fb, fm) |
|||
{ |
|||
return ((b - a) / 6.0) * (fa + (4.0 * fm) + fb) |
|||
} |
|||
function recursive_simpson(a, fa, b, fb, tol, whole, m, fm, depth, |
|||
# Local variables: |
|||
lm, flm, left, |
|||
rm, frm, right, |
|||
delta, tol_, |
|||
leftval, rightval, quadval) |
|||
{ |
|||
lm = midpoint(a, m) |
|||
flm = f(lm) |
|||
left = simpson_rule(a, fa, m, fm, flm) |
|||
rm = midpoint(m, b) |
|||
frm = f(rm) |
|||
right = simpson_rule(m, fm, b, fb, frm) |
|||
delta = left + right - whole |
|||
tol_ = 0.5 * tol |
|||
if (depth <= 0 || tol_ == tol || abs(delta) <= 15.0 * tol) |
|||
quadval = left + right + (delta / 15.0) |
|||
else |
|||
{ |
|||
leftval = recursive_simpson(a, fa, m, fm, tol_, |
|||
left, lm, flm, depth - 1) |
|||
rightval = recursive_simpson(m, fm, b, fb, tol_, |
|||
right, rm, frm, depth - 1) |
|||
quadval = leftval + rightval |
|||
} |
|||
return quadval |
|||
} |
|||
function quad_asr(a, b, tol, depth, |
|||
# Local variables: |
|||
fa, fb, whole, m, fm) |
|||
{ |
|||
fa = f(a) |
|||
fb = f(b) |
|||
m = midpoint(a, b) |
|||
fm = f(m) |
|||
whole = simpson_rule(a, fa, b, fb, fm) |
|||
return recursive_simpson(a, fa, b, fb, tol, |
|||
whole, m, fm, depth) |
|||
} |
|||
function abs(x) |
|||
{ |
|||
return (x < 0 ? -x : x) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
Just about any modern Awk should work: nawk, mawk, gawk, busybox awk, goawk, etc. But the ancient Old Awk does not work. |
|||
<pre>$ nawk -f adaptive_simpson_task.awk |
|||
estimate of ∫ sin x dx from 0 to 1: 0.459698</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |