27 lines
540 B
C
27 lines
540 B
C
#include <math.h>
|
|
#include <stdio.h>
|
|
|
|
#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36)
|
|
|
|
double simpsons_1_3(double a, double b, double n) {
|
|
double h = (b - a) / n;
|
|
|
|
double s = f(a) + f(b);
|
|
|
|
// Add the other heights
|
|
|
|
for (int i = 1; i < n; i++)
|
|
s += 3 * f(a + i * h);
|
|
|
|
for (int i = 3; i < n; i += 3)
|
|
s -= f(a + i * h);
|
|
|
|
return (h / 8) * 3 * s;
|
|
}
|
|
|
|
int main() {
|
|
printf("The area under the curve f(x) = 352x^3 - 64x^2 + 198x - 36 from x=3 "
|
|
"to x=4 is %lf",
|
|
simpsons_1_3(3, 4, 10000));
|
|
}
|