26
7.c
Normal file
26
7.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#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 += 2)
|
||||
s += 4 * f(a + i * h);
|
||||
|
||||
for (int i = 2; i < n; i += 2)
|
||||
s += 2 * f(a + i * h);
|
||||
|
||||
return (h / 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));
|
||||
}
|
Reference in New Issue
Block a user