用C语言表示e^xcos(4x)在[0, π]上的积分的复化梯形公式
时间: 2024-02-11 19:06:12 浏览: 65
以下是用C语言表示e^xcos(4x)在[0, π]上的积分的复化梯形公式的代码:
```c
#include <stdio.h>
#include <math.h>
double f(double x) {
return exp(x) * cos(4 * x);
}
double composite_trapezoidal(double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
sum += 2 * f(x);
}
return sum * h / 2;
}
int main() {
double a = 0.0;
double b = M_PI;
int n = 1000;
double integral = composite_trapezoidal(a, b, n);
printf("The integral of e^xcos(4x) from 0 to pi is approximately %lf\n", integral);
return 0;
}
```
这段代码中,`f`函数表示被积函数e^xcos(4x),`composite_trapezoidal`函数表示复化梯形公式,`main`函数中调用`composite_trapezoidal`函数计算积分,并输出结果。
阅读全文