c program to implement numerical integration using simpson 3/8 rule
code:
#include <stdio.h>
#include <math.h>
double f(double x) {
return exp(x);
}
double simpson38(double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
if (i % 3 == 0) {
sum += 2 * f(a + i * h);
} else {
sum += 3 * f(a + i * h);
}
}
return (3 * h / 8) * sum;
}
int main() {
double a = 0;
double b = 1;
int n = 10;
double integral = simpson38(a, b, n);
printf("The integral of exp(x) from %f to %f is %f\n", a, b, integral);
return 0;
}
output :
The integral of exp(x) from 0 to 1 is 1.718282
.png)
Comments
Post a Comment