Numerical integration: Difference between revisions
Content added Content deleted
m (Already have a link for this) |
No edit summary |
||
Line 261: | Line 261: | ||
{{trans|Java}} |
{{trans|Java}} |
||
<c>#include <math.h> |
<lang c>#include <math.h> |
||
double int_leftrect(double from, double to, double n, double (*func)()) |
double int_leftrect(double from, double to, double n, double (*func)()) |
||
Line 314: | Line 314: | ||
return h / 6.0 * (func(from) + func(to) + 4.0 * sum1 + 2.0 * sum2); |
return h / 6.0 * (func(from) + func(to) + 4.0 * sum1 + 2.0 * sum2); |
||
}</ |
}</lang> |
||
Usage example: |
Usage example: |
||
<c>#include <stdio.h> |
<lang c>#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 370: | Line 370: | ||
printf("\n"); |
printf("\n"); |
||
} |
} |
||
}</ |
}</lang> |
||
Output of the test (with a little bit of enhancing by hand): |
Output of the test (with a little bit of enhancing by hand): |
||
Line 851: | Line 851: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<pascal> |
<lang pascal> |
||
function RectLeft(function f(x: real): real; xl, xr: real): real; |
function RectLeft(function f(x: real): real; xl, xr: real): real; |
||
begin |
begin |
||
Line 893: | Line 893: | ||
integrate := integral |
integrate := integral |
||
end; |
end; |
||
</ |
</lang> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python> |
<lang python> |
||
def left_rect(f,x,h): |
def left_rect(f,x,h): |
||
return f(x) |
return f(x) |
||
Line 921: | Line 921: | ||
t = integrate( square, 3.0, 7.0, 30, simpson ) |
t = integrate( square, 3.0, 7.0, 30, simpson ) |
||
</ |
</lang> |
||
A faster Simpson's rule integrator is |
A faster Simpson's rule integrator is |
||
<python>def faster_simpson(f, a, b, steps): |
<lang python>def faster_simpson(f, a, b, steps): |
||
h = (b-a)/steps |
h = (b-a)/steps |
||
a1 = a+h/2 |
a1 = a+h/2 |
||
Line 929: | Line 929: | ||
s2 = sum( f(a+i*h) for i in range(1,steps)) |
s2 = sum( f(a+i*h) for i in range(1,steps)) |
||
return (h/6.0)*(f(a)+f(b)+4.0*s1+2.0*s2) |
return (h/6.0)*(f(a)+f(b)+4.0*s1+2.0*s2) |
||
</ |
</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |