用C语言写一个不定积分运算程序
时间: 2023-02-23 12:26:53 浏览: 435
下面是一个C语言写的不定积分运算程序的示例:
```
#include <stdio.h>
#include <math.h>
double f(double x) {
return sin(x);
}
double trapezoidal(double a, double b, int n) {
double h = (b - a) / n;
double sum = 0.5 * (f(a) + f(b));
for (int i = 1; i < n; i++) {
sum += f(a + i * h);
}
return sum * h;
}
int main() {
double a, b;
int n;
printf("Enter the limits of integration (a b): ");
scanf("%lf%lf", &a, &b);
printf("Enter the number of intervals: ");
scanf("%d", &n);
printf("The definite integral is: %.10lf\n", trapezoidal(a, b, n));
return 0;
}
```
这个程序使用梯形法来计算不定积分。它接受两个积分限制`a`和`b`以及一个整数`n`,表示分割的区间数。函数`f(x)`是要积分的函数,在这个程序中是`sin(x)`。积分结果将通过调用`trapezoidal()`函数来计算,并在最后输出。